Comparing version 0.0.9-10 to 0.0.9-11
@@ -49,2 +49,11 @@ import { BlockKinds, Block, TypedBlock, Module } from "./types"; | ||
}): UnionTypeBlock; | ||
declare type UnionUntaggedTypeBlock = { | ||
kind: "UnionUntaggedTypeBlock"; | ||
lineStart: number; | ||
lines: string[]; | ||
}; | ||
declare function UnionUntaggedTypeBlock(args: { | ||
lineStart: number; | ||
lines: string[]; | ||
}): UnionUntaggedTypeBlock; | ||
declare type TypeAliasBlock = { | ||
@@ -104,3 +113,3 @@ kind: "TypeAliasBlock"; | ||
}): UnknownBlock; | ||
declare type UnparsedBlock = ImportBlock | ExportBlock | UnionTypeBlock | TypeAliasBlock | FunctionBlock | ConstBlock | CommentBlock | MultilineCommentBlock | UnknownBlock; | ||
declare type UnparsedBlock = ImportBlock | ExportBlock | UnionTypeBlock | UnionUntaggedTypeBlock | TypeAliasBlock | FunctionBlock | ConstBlock | CommentBlock | MultilineCommentBlock | UnknownBlock; | ||
declare function blockKind(block: string): Result<string, BlockKinds>; | ||
@@ -107,0 +116,0 @@ declare function createUnparsedBlock(blockKind: BlockKinds, lineStart: number, lines: string[]): UnparsedBlock; |
@@ -43,2 +43,5 @@ "use strict"; | ||
} | ||
function UnionUntaggedTypeBlock(args) { | ||
return Object.assign({ kind: "UnionUntaggedTypeBlock" }, args); | ||
} | ||
function TypeAliasBlock(args) { | ||
@@ -118,2 +121,7 @@ return Object.assign({ kind: "TypeAliasBlock" }, args); | ||
test: function (x) { | ||
return x.startsWith("type ") && x.includes(`"`); | ||
}, | ||
blockKind: "UnionUntaggedType" | ||
}, { | ||
test: function (x) { | ||
return x.startsWith("type "); | ||
@@ -210,2 +218,8 @@ }, | ||
} | ||
case "UnionUntaggedType": { | ||
return UnionUntaggedTypeBlock({ | ||
lineStart, | ||
lines | ||
}); | ||
} | ||
case "TypeAlias": { | ||
@@ -311,3 +325,24 @@ return TypeAliasBlock({ | ||
const { value } = info.currentBlockKind; | ||
const block = createUnparsedBlock(value, info.lineStart, info.currentBlock); | ||
const hasSpeech = (function (y) { | ||
return y.length > 0; | ||
})(info.currentBlock.filter(function (line) { | ||
return line.indexOf(`"`) > -1; | ||
})); | ||
const kind = (function () { | ||
switch (value) { | ||
case "UnionType": { | ||
if (hasSpeech) { | ||
return "UnionUntaggedType"; | ||
} | ||
else { | ||
return value; | ||
} | ||
; | ||
} | ||
default: { | ||
return value; | ||
} | ||
} | ||
})(); | ||
const block = createUnparsedBlock(kind, info.lineStart, info.currentBlock); | ||
const nextInfo = { | ||
@@ -370,3 +405,3 @@ previousLine: line, | ||
return List.filter(function (block) { | ||
return block.kind === "UnionType" || block.kind === "TypeAlias"; | ||
return block.kind === "UnionType" || block.kind === "TypeAlias" || block.kind === "UnionUntaggedType"; | ||
}, blocks); | ||
@@ -373,0 +408,0 @@ } |
@@ -177,2 +177,5 @@ "use strict"; | ||
} | ||
case "UnionUntaggedType": { | ||
return []; | ||
} | ||
} | ||
@@ -219,2 +222,5 @@ } | ||
} | ||
case "UnionUntaggedType": { | ||
return []; | ||
} | ||
} | ||
@@ -249,2 +255,5 @@ } | ||
} | ||
case "UnionUntaggedType": { | ||
return []; | ||
} | ||
} | ||
@@ -251,0 +260,0 @@ } |
@@ -51,2 +51,9 @@ "use strict"; | ||
} | ||
function generateUnionUntaggedType(syntax) { | ||
const values = (function (y) { | ||
return y.join("\n| "); | ||
})(List.map(generateStringValue, syntax.values)); | ||
const prefixed = Common_1.prefixLines(values, 4); | ||
return `type ${generateType(syntax.type)} =\n${prefixed}`; | ||
} | ||
function generateProperty(syntax) { | ||
@@ -776,2 +783,5 @@ const generatedType = generateTopLevelType(syntax.type); | ||
} | ||
case "UnionUntaggedType": { | ||
return generateUnionUntaggedType(syntax); | ||
} | ||
case "TypeAlias": { | ||
@@ -778,0 +788,0 @@ return generateTypeAlias(syntax); |
@@ -51,2 +51,9 @@ "use strict"; | ||
} | ||
function generateUnionUntaggedType(syntax) { | ||
const values = (function (y) { | ||
return y.join("\n| "); | ||
})(List.map(generateStringValue, syntax.values)); | ||
const prefixed = Common_1.prefixLines(values, 4); | ||
return `type ${generateType(syntax.type)} =\n${prefixed}`; | ||
} | ||
function generateProperty(syntax) { | ||
@@ -794,2 +801,5 @@ const generatedType = generateTopLevelType(syntax.type); | ||
} | ||
case "UnionUntaggedType": { | ||
return generateUnionUntaggedType(syntax); | ||
} | ||
case "TypeAlias": { | ||
@@ -796,0 +806,0 @@ return generateTypeAlias(syntax); |
@@ -50,2 +50,9 @@ "use strict"; | ||
} | ||
function generateUnionUntaggedType(syntax) { | ||
const values = (function (y) { | ||
return y.join("\n| "); | ||
})(List.map(generateStringValue, syntax.values)); | ||
const prefixed = Common_1.prefixLines(values, 4); | ||
return `type ${generateType(syntax.type)} =\n${prefixed}`; | ||
} | ||
function generateProperty(syntax) { | ||
@@ -631,2 +638,5 @@ return `${syntax.name}: ${generateType(syntax.type)}`; | ||
} | ||
case "UnionUntaggedType": { | ||
return generateUnionUntaggedType(syntax); | ||
} | ||
case "TypeAlias": { | ||
@@ -633,0 +643,0 @@ return generateTypeAlias(syntax); |
@@ -800,2 +800,5 @@ "use strict"; | ||
} | ||
case "UnionUntaggedType": { | ||
return ""; | ||
} | ||
case "TypeAlias": { | ||
@@ -802,0 +805,0 @@ return generateTypeAlias(syntax); |
@@ -134,2 +134,10 @@ "use strict"; | ||
} | ||
function generateUnionUntaggedType(syntax) { | ||
const values = (function (y) { | ||
return y.join(" | "); | ||
})(List.map(function (y) { | ||
return `"${y.body}"`; | ||
}, syntax.values)); | ||
return `type ${generateType(syntax.type, [])} = ${values};`; | ||
} | ||
function generateProperty(syntax, imports) { | ||
@@ -1216,2 +1224,5 @@ return `${syntax.name}: ${generateTopLevelType(syntax.type, imports)}`; | ||
} | ||
case "UnionUntaggedType": { | ||
return generateUnionUntaggedType(syntax); | ||
} | ||
case "TypeAlias": { | ||
@@ -1218,0 +1229,0 @@ return generateTypeAlias(syntax, actualImports); |
@@ -95,4 +95,4 @@ import { Result } from "@eeue56/ts-core/build/main/lib/result"; | ||
export declare function FunctionTypeToken(body: TypeToken[]): FunctionTypeToken; | ||
export declare type TypeTokenRaw = IdentifierToken | ArrowToken | OpenBracketToken | CloseBracketToken; | ||
export declare type TypeToken = IdentifierToken | ArrowToken | OpenBracketToken | CloseBracketToken | BaseTypeToken | FunctionTypeToken; | ||
export declare type TypeTokenRaw = IdentifierToken | ArrowToken | OpenBracketToken | CloseBracketToken | StringToken; | ||
export declare type TypeToken = IdentifierToken | ArrowToken | OpenBracketToken | CloseBracketToken | StringToken | BaseTypeToken | FunctionTypeToken; | ||
export declare type RootTypeTokens = BaseTypeToken | FunctionTypeToken; | ||
@@ -99,0 +99,0 @@ export declare function tokenizeType(tokens: Token[]): Result<string, RootTypeTokens[]>; |
@@ -616,2 +616,5 @@ "use strict"; | ||
} | ||
case "StringToken": { | ||
currentBuffer.push(token); | ||
} | ||
default: | ||
@@ -735,2 +738,5 @@ continue; | ||
} | ||
case "StringToken": { | ||
return `${token.body}`; | ||
} | ||
} | ||
@@ -737,0 +743,0 @@ } |
@@ -424,2 +424,79 @@ "use strict"; | ||
} | ||
function finalExpressions(expression) { | ||
switch (expression.kind) { | ||
case "Value": | ||
return []; | ||
case "StringValue": | ||
return [expression.body]; | ||
case "FormatStringValue": | ||
return []; | ||
case "ListValue": | ||
return []; | ||
case "ListRange": | ||
return []; | ||
case "ObjectLiteral": | ||
return []; | ||
case "IfStatement": | ||
return finalExpressions(expression.ifBody).concat(finalExpressions(expression.elseBody)); | ||
case "CaseStatement": | ||
let expressions = []; | ||
for (const branch of expression.branches) { | ||
expressions = expressions.concat(finalExpressions(branch.body)); | ||
} | ||
return expressions; | ||
case "Addition": | ||
return []; | ||
case "Subtraction": | ||
return []; | ||
case "Multiplication": | ||
return []; | ||
case "Division": | ||
return []; | ||
case "And": | ||
return []; | ||
case "Or": | ||
return []; | ||
case "ListPrepend": | ||
return []; | ||
case "LeftPipe": | ||
return []; | ||
case "RightPipe": | ||
return []; | ||
case "ModuleReference": | ||
return []; | ||
case "FunctionCall": | ||
return []; | ||
case "Lambda": | ||
return []; | ||
case "LambdaCall": | ||
return []; | ||
case "Constructor": | ||
return []; | ||
case "Equality": | ||
return []; | ||
case "InEquality": | ||
return []; | ||
case "LessThan": | ||
return []; | ||
case "LessThanOrEqual": | ||
return []; | ||
case "GreaterThan": | ||
return []; | ||
case "GreaterThanOrEqual": | ||
return []; | ||
} | ||
} | ||
function allFinalExpressions(block) { | ||
switch (block.kind) { | ||
case "Const": { | ||
return finalExpressions(block.value); | ||
} | ||
case "Function": { | ||
return finalExpressions(block.body); | ||
} | ||
default: { | ||
return []; | ||
} | ||
} | ||
} | ||
function validateType(block, typedBlocks, imports) { | ||
@@ -438,2 +515,22 @@ switch (block.kind) { | ||
} | ||
if (inferred.kind === "FixedType" && inferred.name === "string") { | ||
if (block.type.kind === "FixedType") { | ||
const matchingBlocks = typedBlocks.filter((b) => isSameType(b.type, block.type, false)); | ||
if (matchingBlocks.length > 0) { | ||
const matchingBlock = matchingBlocks[0]; | ||
if (matchingBlock.kind === "UnionUntaggedType") { | ||
const finalExpressions = allFinalExpressions(block); | ||
for (const finalExpression of finalExpressions) { | ||
const isCovered = matchingBlock.values.filter((v) => v.body === finalExpression).length > 0; | ||
if (!isCovered) { | ||
return result_1.Err(`Expected \`${typeToString(block.type)}, composed of ${matchingBlock.values | ||
.map((v) => `"${v.body}"`) | ||
.join(" | ")}\` but got \`${finalExpression}\` in the body of the function`); | ||
} | ||
} | ||
return result_1.Ok(block.type); | ||
} | ||
} | ||
} | ||
} | ||
return result_1.Err(`Expected \`${typeToString(block.type)}\` but got \`${typeToString(inferred)}\``); | ||
@@ -458,2 +555,22 @@ } | ||
const inferred = inferredRes.value; | ||
if (inferred.kind === "FixedType" && inferred.name === "string") { | ||
if (block.returnType.kind === "FixedType") { | ||
const matchingBlocks = typedBlocks.filter((b) => isSameType(b.type, block.returnType, false)); | ||
if (matchingBlocks.length > 0) { | ||
const matchingBlock = matchingBlocks[0]; | ||
if (matchingBlock.kind === "UnionUntaggedType") { | ||
const finalExpressions = allFinalExpressions(block); | ||
for (const finalExpression of finalExpressions) { | ||
const isCovered = matchingBlock.values.filter((v) => v.body === finalExpression).length > 0; | ||
if (!isCovered) { | ||
return result_1.Err(`Expected \`${typeToString(block.returnType)}, composed of ${matchingBlock.values | ||
.map((v) => `"${v.body}"`) | ||
.join(" | ")}\` but got \`${finalExpression}\` in the body of the function`); | ||
} | ||
} | ||
return result_1.Ok(block.returnType); | ||
} | ||
} | ||
} | ||
} | ||
if (!isSameType(block.returnType, inferred, false)) { | ||
@@ -465,2 +582,3 @@ return result_1.Err(`Expected \`${typeToString(block.returnType)}\` but got \`${typeToString(inferred)}\` in the body of the function`); | ||
case "UnionType": | ||
case "UnionUntaggedType": | ||
case "TypeAlias": { | ||
@@ -467,0 +585,0 @@ return result_1.Ok(block.type); |
@@ -37,2 +37,8 @@ import { Maybe } from "@eeue56/ts-core/build/main/lib/maybe"; | ||
export declare function UnionType(type: FixedType, tags: Tag[]): UnionType; | ||
export declare type UnionUntaggedType = { | ||
kind: "UnionUntaggedType"; | ||
type: FixedType; | ||
values: StringValue[]; | ||
}; | ||
export declare function UnionUntaggedType(type: FixedType, values: StringValue[]): UnionUntaggedType; | ||
export declare type Property = { | ||
@@ -317,3 +323,3 @@ kind: "Property"; | ||
export declare function MultilineComment(body: string): MultilineComment; | ||
export declare type UnparsedBlockTypes = "ImportBlock" | "ExportBlock" | "UnionTypeBlock" | "TypeAliasBlock" | "FunctionBlock" | "ConstBlock" | "CommentBlock" | "MultilineCommentBlock" | "UnknownBlock"; | ||
export declare type UnparsedBlockTypes = "ImportBlock" | "ExportBlock" | "UnionTypeBlock" | "TypeAliasBlock" | "UnionUntaggedTypeBlock" | "FunctionBlock" | "ConstBlock" | "CommentBlock" | "MultilineCommentBlock" | "UnknownBlock"; | ||
export declare type UnparsedBlock = { | ||
@@ -325,5 +331,5 @@ kind: UnparsedBlockTypes; | ||
export declare function UnparsedBlock(kind: UnparsedBlockTypes, lineStart: number, lines: string[]): UnparsedBlock; | ||
export declare type BlockKinds = "Import" | "Export" | "UnionType" | "TypeAlias" | "Function" | "Const" | "Indent" | "Definition" | "Comment" | "MultilineComment" | "Unknown"; | ||
export declare type Block = UnionType | TypeAlias | Function | Const | Import | Export | Comment | MultilineComment; | ||
export declare type TypedBlock = UnionType | TypeAlias; | ||
export declare type BlockKinds = "Import" | "Export" | "UnionType" | "UnionUntaggedType" | "TypeAlias" | "Function" | "Const" | "Indent" | "Definition" | "Comment" | "MultilineComment" | "Unknown"; | ||
export declare type Block = UnionType | UnionUntaggedType | TypeAlias | Function | Const | Import | Export | Comment | MultilineComment; | ||
export declare type TypedBlock = UnionType | TypeAlias | UnionUntaggedType; | ||
export declare type DoExpression = FunctionCall | ModuleReference | IfStatement | Const | Function; | ||
@@ -330,0 +336,0 @@ export declare type DoBlock = { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Import = exports.ImportModule = exports.Const = exports.Function = exports.isLeftPipeableExpression = exports.isSimpleValue = exports.GreaterThanOrEqual = exports.GreaterThan = exports.LessThanOrEqual = exports.LessThan = exports.InEquality = exports.Equality = exports.CaseStatement = exports.Branch = exports.ListDestructure = exports.EmptyList = exports.Default = exports.LambdaCall = exports.Lambda = exports.FunctionCall = exports.ModuleReference = exports.RightPipe = exports.LeftPipe = exports.ListPrepend = exports.Or = exports.And = exports.Division = exports.Multiplication = exports.Subtraction = exports.Addition = exports.IfStatement = exports.Constructor = exports.Destructure = exports.FormatStringValue = exports.ListRange = exports.ListValue = exports.StringValue = exports.ObjectLiteral = exports.Field = exports.Value = exports.AnonFunctionArg = exports.FunctionArg = exports.TypeAlias = exports.Property = exports.UnionType = exports.Tag = exports.TagArg = exports.FunctionType = exports.FixedType = exports.GenericType = void 0; | ||
exports.contextModuleToModule = exports.ContextModule = exports.Module = exports.DoBlock = exports.UnparsedBlock = exports.MultilineComment = exports.Comment = exports.Export = void 0; | ||
exports.ImportModule = exports.Const = exports.Function = exports.isLeftPipeableExpression = exports.isSimpleValue = exports.GreaterThanOrEqual = exports.GreaterThan = exports.LessThanOrEqual = exports.LessThan = exports.InEquality = exports.Equality = exports.CaseStatement = exports.Branch = exports.ListDestructure = exports.EmptyList = exports.Default = exports.LambdaCall = exports.Lambda = exports.FunctionCall = exports.ModuleReference = exports.RightPipe = exports.LeftPipe = exports.ListPrepend = exports.Or = exports.And = exports.Division = exports.Multiplication = exports.Subtraction = exports.Addition = exports.IfStatement = exports.Constructor = exports.Destructure = exports.FormatStringValue = exports.ListRange = exports.ListValue = exports.StringValue = exports.ObjectLiteral = exports.Field = exports.Value = exports.AnonFunctionArg = exports.FunctionArg = exports.TypeAlias = exports.Property = exports.UnionUntaggedType = exports.UnionType = exports.Tag = exports.TagArg = exports.FunctionType = exports.FixedType = exports.GenericType = void 0; | ||
exports.contextModuleToModule = exports.ContextModule = exports.Module = exports.DoBlock = exports.UnparsedBlock = exports.MultilineComment = exports.Comment = exports.Export = exports.Import = void 0; | ||
function GenericType(name) { | ||
@@ -51,2 +51,10 @@ return { | ||
exports.UnionType = UnionType; | ||
function UnionUntaggedType(type, values) { | ||
return { | ||
kind: "UnionUntaggedType", | ||
type, | ||
values, | ||
}; | ||
} | ||
exports.UnionUntaggedType = UnionUntaggedType; | ||
function Property(name, type) { | ||
@@ -53,0 +61,0 @@ return { |
{ | ||
"name": "derw", | ||
"version": "0.0.9-10", | ||
"version": "0.0.9-11", | ||
"description": "An Elm-inspired language that transpiles to TypeScript", | ||
@@ -11,2 +11,3 @@ "main": "index.js", | ||
"test": "npx @eeue56/bach", | ||
"test-chromebook": "./test_on_chromebook.sh", | ||
"bench": "npx @eeue56/mainc", | ||
@@ -40,3 +41,3 @@ "format": "npx prettier --write .", | ||
"@eeue56/adeilad": "^0.0.2", | ||
"@eeue56/bach": "^0.0.12", | ||
"@eeue56/bach": "^0.0.13-0", | ||
"@eeue56/baner": "^0.0.3", | ||
@@ -43,0 +44,0 @@ "@eeue56/ts-core": "^3.0.1", |
@@ -89,2 +89,15 @@ import { isTestFile } from "./Utils"; | ||
type UnionUntaggedTypeBlock = { | ||
kind: "UnionUntaggedTypeBlock"; | ||
lineStart: number; | ||
lines: string[]; | ||
}; | ||
function UnionUntaggedTypeBlock(args: { lineStart: number, lines: string[] }): UnionUntaggedTypeBlock { | ||
return { | ||
kind: "UnionUntaggedTypeBlock", | ||
...args, | ||
}; | ||
} | ||
type TypeAliasBlock = { | ||
@@ -168,3 +181,3 @@ kind: "TypeAliasBlock"; | ||
type UnparsedBlock = ImportBlock | ExportBlock | UnionTypeBlock | TypeAliasBlock | FunctionBlock | ConstBlock | CommentBlock | MultilineCommentBlock | UnknownBlock; | ||
type UnparsedBlock = ImportBlock | ExportBlock | UnionTypeBlock | UnionUntaggedTypeBlock | TypeAliasBlock | FunctionBlock | ConstBlock | CommentBlock | MultilineCommentBlock | UnknownBlock; | ||
@@ -229,2 +242,7 @@ function hasTypeLine(block: string): boolean { | ||
test: function(x: any) { | ||
return x.startsWith("type ") && x.includes(`"`); | ||
}, | ||
blockKind: "UnionUntaggedType" | ||
}, { | ||
test: function(x: any) { | ||
return x.startsWith("type "); | ||
@@ -321,2 +339,8 @@ }, | ||
} | ||
case "UnionUntaggedType": { | ||
return UnionUntaggedTypeBlock({ | ||
lineStart, | ||
lines | ||
}); | ||
} | ||
case "TypeAlias": { | ||
@@ -442,3 +466,22 @@ return TypeAliasBlock({ | ||
const { value } = info.currentBlockKind; | ||
const block: UnparsedBlock = createUnparsedBlock(value, info.lineStart, info.currentBlock); | ||
const hasSpeech: boolean = (function(y: any) { | ||
return y.length > 0; | ||
})(info.currentBlock.filter(function(line: any) { | ||
return line.indexOf(`"`) > -1; | ||
})); | ||
const kind: BlockKinds = (function (): any { | ||
switch (value) { | ||
case "UnionType": { | ||
if (hasSpeech) { | ||
return "UnionUntaggedType"; | ||
} else { | ||
return value; | ||
}; | ||
} | ||
default: { | ||
return value; | ||
} | ||
} | ||
})(); | ||
const block: UnparsedBlock = createUnparsedBlock(kind, info.lineStart, info.currentBlock); | ||
const nextInfo: IntoBlockInfo = { | ||
@@ -498,3 +541,3 @@ previousLine: line, | ||
return List.filter(function(block: any) { | ||
return block.kind === "UnionType" || block.kind === "TypeAlias"; | ||
return block.kind === "UnionType" || block.kind === "TypeAlias" || block.kind === "UnionUntaggedType"; | ||
}, blocks); | ||
@@ -501,0 +544,0 @@ } |
@@ -179,2 +179,5 @@ import { Block, ContextModule, Expression, Value } from "../types"; | ||
} | ||
case "UnionUntaggedType": { | ||
return [ ]; | ||
} | ||
} | ||
@@ -222,2 +225,5 @@ } | ||
} | ||
case "UnionUntaggedType": { | ||
return [ ]; | ||
} | ||
} | ||
@@ -252,2 +258,5 @@ } | ||
} | ||
case "UnionUntaggedType": { | ||
return [ ]; | ||
} | ||
} | ||
@@ -254,0 +263,0 @@ } |
@@ -30,3 +30,3 @@ import * as List from "../stdlib/List"; | ||
import { Tag, UnionType, Type, TagArg, Block, Constructor, Expression, isSimpleValue } from "../types"; | ||
import { Tag, UnionType, UnionUntaggedType, Type, TagArg, Block, Constructor, Expression, isSimpleValue } from "../types"; | ||
@@ -64,2 +64,10 @@ import { prefixLines } from "./Common"; | ||
function generateUnionUntaggedType(syntax: UnionUntaggedType): string { | ||
const values: string = (function(y: any) { | ||
return y.join("\n| "); | ||
})(List.map(generateStringValue, syntax.values)); | ||
const prefixed: string = prefixLines(values, 4); | ||
return `type ${generateType(syntax.type)} =\n${prefixed}`; | ||
} | ||
function generateProperty(syntax: Property): string { | ||
@@ -814,2 +822,5 @@ const generatedType: string = generateTopLevelType(syntax.type); | ||
} | ||
case "UnionUntaggedType": { | ||
return generateUnionUntaggedType(syntax); | ||
} | ||
case "TypeAlias": { | ||
@@ -816,0 +827,0 @@ return generateTypeAlias(syntax); |
@@ -30,3 +30,3 @@ import * as List from "../stdlib/List"; | ||
import { Tag, UnionType, Type, TagArg, Block, Constructor, Expression, isSimpleValue } from "../types"; | ||
import { Tag, UnionType, UnionUntaggedType, Type, TagArg, Block, Constructor, Expression, isSimpleValue } from "../types"; | ||
@@ -64,2 +64,10 @@ import { prefixLines } from "./Common"; | ||
function generateUnionUntaggedType(syntax: UnionUntaggedType): string { | ||
const values: string = (function(y: any) { | ||
return y.join("\n| "); | ||
})(List.map(generateStringValue, syntax.values)); | ||
const prefixed: string = prefixLines(values, 4); | ||
return `type ${generateType(syntax.type)} =\n${prefixed}`; | ||
} | ||
function generateProperty(syntax: Property): string { | ||
@@ -825,2 +833,5 @@ const generatedType: string = generateTopLevelType(syntax.type); | ||
} | ||
case "UnionUntaggedType": { | ||
return generateUnionUntaggedType(syntax); | ||
} | ||
case "TypeAlias": { | ||
@@ -827,0 +838,0 @@ return generateTypeAlias(syntax); |
@@ -27,3 +27,3 @@ import * as List from "../stdlib/List"; | ||
import { Tag, UnionType, Type, TagArg, Block, Constructor, Expression } from "../types"; | ||
import { Tag, UnionType, UnionUntaggedType, Type, TagArg, Block, Constructor, Expression } from "../types"; | ||
@@ -61,2 +61,10 @@ import { prefixLines } from "./Common"; | ||
function generateUnionUntaggedType(syntax: UnionUntaggedType): string { | ||
const values: string = (function(y: any) { | ||
return y.join("\n| "); | ||
})(List.map(generateStringValue, syntax.values)); | ||
const prefixed: string = prefixLines(values, 4); | ||
return `type ${generateType(syntax.type)} =\n${prefixed}`; | ||
} | ||
function generateProperty(syntax: Property): string { | ||
@@ -665,2 +673,5 @@ return `${syntax.name}: ${generateType(syntax.type)}`; | ||
} | ||
case "UnionUntaggedType": { | ||
return generateUnionUntaggedType(syntax); | ||
} | ||
case "TypeAlias": { | ||
@@ -667,0 +678,0 @@ return generateTypeAlias(syntax); |
@@ -852,2 +852,5 @@ import { exportTests } from "../Blocks"; | ||
} | ||
case "UnionUntaggedType": { | ||
return ""; | ||
} | ||
case "TypeAlias": { | ||
@@ -854,0 +857,0 @@ return generateTypeAlias(syntax); |
@@ -33,3 +33,3 @@ import { exportTests } from "../Blocks"; | ||
import { Tag, UnionType, Type, TagArg, Block, Constructor, Expression, FunctionType, GenericType, FixedType, isSimpleValue } from "../types"; | ||
import { Tag, UnionType, UnionUntaggedType, Type, TagArg, Block, Constructor, Expression, FunctionType, GenericType, FixedType, isSimpleValue } from "../types"; | ||
@@ -148,2 +148,11 @@ import { prefixLines, destructureLength, patternHasGaps, patternGapPositions } from "./Common"; | ||
function generateUnionUntaggedType(syntax: UnionUntaggedType): string { | ||
const values: string = (function(y: any) { | ||
return y.join(" | "); | ||
})(List.map(function(y: any) { | ||
return `"${y.body}"`; | ||
}, syntax.values)); | ||
return `type ${generateType(syntax.type, [])} = ${values};`; | ||
} | ||
function generateProperty(syntax: Property, imports: Import[]): string { | ||
@@ -1254,2 +1263,5 @@ return `${syntax.name}: ${generateTopLevelType(syntax.type, imports)}`; | ||
} | ||
case "UnionUntaggedType": { | ||
return generateUnionUntaggedType(syntax); | ||
} | ||
case "TypeAlias": { | ||
@@ -1256,0 +1268,0 @@ return generateTypeAlias(syntax, actualImports); |
@@ -750,3 +750,4 @@ import { Ok, Result } from "@eeue56/ts-core/build/main/lib/result"; | ||
| OpenBracketToken | ||
| CloseBracketToken; | ||
| CloseBracketToken | ||
| StringToken; | ||
@@ -758,2 +759,3 @@ export type TypeToken = | ||
| CloseBracketToken | ||
| StringToken | ||
| BaseTypeToken | ||
@@ -811,2 +813,6 @@ | FunctionTypeToken; | ||
} | ||
case "StringToken": { | ||
currentBuffer.push(token); | ||
} | ||
default: | ||
@@ -924,2 +930,5 @@ continue; | ||
} | ||
case "StringToken": { | ||
return `${token.body}`; | ||
} | ||
} | ||
@@ -926,0 +935,0 @@ } |
@@ -648,2 +648,84 @@ import { Err, Ok, Result } from "@eeue56/ts-core/build/main/lib/result"; | ||
function finalExpressions(expression: Expression): string[] { | ||
switch (expression.kind) { | ||
case "Value": | ||
return [ ]; | ||
case "StringValue": | ||
return [ expression.body ]; | ||
case "FormatStringValue": | ||
return [ ]; | ||
case "ListValue": | ||
return [ ]; | ||
case "ListRange": | ||
return [ ]; | ||
case "ObjectLiteral": | ||
return [ ]; | ||
case "IfStatement": | ||
return finalExpressions(expression.ifBody).concat( | ||
finalExpressions(expression.elseBody) | ||
); | ||
case "CaseStatement": | ||
let expressions: string[] = [ ]; | ||
for (const branch of expression.branches) { | ||
expressions = expressions.concat(finalExpressions(branch.body)); | ||
} | ||
return expressions; | ||
case "Addition": | ||
return [ ]; | ||
case "Subtraction": | ||
return [ ]; | ||
case "Multiplication": | ||
return [ ]; | ||
case "Division": | ||
return [ ]; | ||
case "And": | ||
return [ ]; | ||
case "Or": | ||
return [ ]; | ||
case "ListPrepend": | ||
return [ ]; | ||
case "LeftPipe": | ||
return [ ]; | ||
case "RightPipe": | ||
return [ ]; | ||
case "ModuleReference": | ||
return [ ]; | ||
case "FunctionCall": | ||
return [ ]; | ||
case "Lambda": | ||
return [ ]; | ||
case "LambdaCall": | ||
return [ ]; | ||
case "Constructor": | ||
return [ ]; | ||
case "Equality": | ||
return [ ]; | ||
case "InEquality": | ||
return [ ]; | ||
case "LessThan": | ||
return [ ]; | ||
case "LessThanOrEqual": | ||
return [ ]; | ||
case "GreaterThan": | ||
return [ ]; | ||
case "GreaterThanOrEqual": | ||
return [ ]; | ||
} | ||
} | ||
function allFinalExpressions(block: Block): string[] { | ||
switch (block.kind) { | ||
case "Const": { | ||
return finalExpressions(block.value); | ||
} | ||
case "Function": { | ||
return finalExpressions(block.body); | ||
} | ||
default: { | ||
return [ ]; | ||
} | ||
} | ||
} | ||
export function validateType( | ||
@@ -671,2 +753,37 @@ block: Block, | ||
if (inferred.kind === "FixedType" && inferred.name === "string") { | ||
if (block.type.kind === "FixedType") { | ||
const matchingBlocks = typedBlocks.filter((b) => | ||
isSameType(b.type, block.type, false) | ||
); | ||
if (matchingBlocks.length > 0) { | ||
const matchingBlock = matchingBlocks[0]; | ||
if (matchingBlock.kind === "UnionUntaggedType") { | ||
const finalExpressions = allFinalExpressions(block); | ||
for (const finalExpression of finalExpressions) { | ||
const isCovered = | ||
matchingBlock.values.filter( | ||
(v) => v.body === finalExpression | ||
).length > 0; | ||
if (!isCovered) { | ||
return Err( | ||
`Expected \`${typeToString( | ||
block.type | ||
)}, composed of ${matchingBlock.values | ||
.map((v) => `"${v.body}"`) | ||
.join( | ||
" | " | ||
)}\` but got \`${finalExpression}\` in the body of the function` | ||
); | ||
} | ||
} | ||
return Ok(block.type); | ||
} | ||
} | ||
} | ||
} | ||
return Err( | ||
@@ -710,2 +827,37 @@ `Expected \`${typeToString( | ||
if (inferred.kind === "FixedType" && inferred.name === "string") { | ||
if (block.returnType.kind === "FixedType") { | ||
const matchingBlocks = typedBlocks.filter((b) => | ||
isSameType(b.type, block.returnType, false) | ||
); | ||
if (matchingBlocks.length > 0) { | ||
const matchingBlock = matchingBlocks[0]; | ||
if (matchingBlock.kind === "UnionUntaggedType") { | ||
const finalExpressions = allFinalExpressions(block); | ||
for (const finalExpression of finalExpressions) { | ||
const isCovered = | ||
matchingBlock.values.filter( | ||
(v) => v.body === finalExpression | ||
).length > 0; | ||
if (!isCovered) { | ||
return Err( | ||
`Expected \`${typeToString( | ||
block.returnType | ||
)}, composed of ${matchingBlock.values | ||
.map((v) => `"${v.body}"`) | ||
.join( | ||
" | " | ||
)}\` but got \`${finalExpression}\` in the body of the function` | ||
); | ||
} | ||
} | ||
return Ok(block.returnType); | ||
} | ||
} | ||
} | ||
} | ||
if (!isSameType(block.returnType, inferred, false)) { | ||
@@ -725,2 +877,3 @@ return Err( | ||
case "UnionType": | ||
case "UnionUntaggedType": | ||
case "TypeAlias": { | ||
@@ -727,0 +880,0 @@ return Ok(block.type); |
@@ -85,2 +85,19 @@ import { Maybe } from "@eeue56/ts-core/build/main/lib/maybe"; | ||
export type UnionUntaggedType = { | ||
kind: "UnionUntaggedType"; | ||
type: FixedType; | ||
values: StringValue[]; | ||
}; | ||
export function UnionUntaggedType( | ||
type: FixedType, | ||
values: StringValue[] | ||
): UnionUntaggedType { | ||
return { | ||
kind: "UnionUntaggedType", | ||
type, | ||
values, | ||
}; | ||
} | ||
export type Property = { | ||
@@ -899,2 +916,3 @@ kind: "Property"; | ||
| "TypeAliasBlock" | ||
| "UnionUntaggedTypeBlock" | ||
| "FunctionBlock" | ||
@@ -928,2 +946,3 @@ | "ConstBlock" | ||
| "UnionType" | ||
| "UnionUntaggedType" | ||
| "TypeAlias" | ||
@@ -940,2 +959,3 @@ | "Function" | ||
| UnionType | ||
| UnionUntaggedType | ||
| TypeAlias | ||
@@ -949,3 +969,3 @@ | Function | ||
export type TypedBlock = UnionType | TypeAlias; | ||
export type TypedBlock = UnionType | TypeAlias | UnionUntaggedType; | ||
@@ -952,0 +972,0 @@ export type DoExpression = |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
1151636
133
26430
+ Added@eeue56/bach@0.0.13(transitive)
- Removed@eeue56/bach@0.0.12(transitive)
Updated@eeue56/bach@^0.0.13-0