Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[VOI-33] Unions #47

Merged
merged 7 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 45 additions & 44 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"@vitest/ui": "^2.0.5",
"eslint": "^7.10.0",
"ts-node": "^10.9.2",
"typescript": "^4.9.3",
"typescript": "^5.6.2",
"vitest": "^2.0.5"
},
"dependencies": {
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/compiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ describe("E2E Compiler Pipeline", () => {
17,
82,
3,
42,
]);
});

Expand Down
23 changes: 23 additions & 0 deletions src/__tests__/fixtures/e2e-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,29 @@ fn get_z_for_gen_str_ta(v: GenericStructuralTypeAlias<i32>) -> i32

pub fn test16() -> i32
get_z_for_gen_str_ta({ x: 1, y: 2, z: 3 })

obj None {}
obj Some<T> { value: T }

type Optional<T> = Some<T> | None

fn optional_match_chain()
let some: Optional<i32> = Some<i32> { value: 39 }

some
.match(x)
Some<i32>: Some<i32> { value: x.value + 1 }
None: None {}
.match(val)
Some<i32>: Some<i32> { value: val.value + 2 }
None: None {}

pub fn test17()
let x = optional_match_chain()

match(x)
Some<i32>: x.value
None: -1
`;

export const tcoText = `
Expand Down
31 changes: 29 additions & 2 deletions src/assembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
ObjectType,
DsArrayType,
voidBaseObject,
UnionType,
} from "./syntax-objects/types.js";
import { Variable } from "./syntax-objects/variable.js";
import { Block } from "./syntax-objects/block.js";
Expand Down Expand Up @@ -162,9 +163,22 @@ const compileMatch = (opts: CompileExprOpts<Match>) => {
);
};

return constructIfChain(
const ifChain = constructIfChain(
expr.defaultCase ? [...expr.cases, expr.defaultCase] : expr.cases
);

if (expr.bindVariable) {
return opts.mod.block(null, [
compileVariable({
...opts,
isReturnExpr: false,
expr: expr.bindVariable,
}),
ifChain,
]);
}

return ifChain;
};

const compileIdentifier = (opts: CompileExprOpts<Identifier>) => {
Expand Down Expand Up @@ -345,7 +359,7 @@ const compileFunction = (opts: CompileExprOpts<Fn>): number => {
isReturnExpr: true,
});

const variableTypes = getFunctionVarTypes(opts, fn); // TODO: Vars should probably be registered with the function type rather than body (for consistency).
const variableTypes = getFunctionVarTypes(opts, fn);

mod.addFunction(fn.id, parameterTypes, returnType, variableTypes, body);

Expand Down Expand Up @@ -446,6 +460,7 @@ export const mapBinaryenType = (
if (isPrimitiveId(type, "f64")) return binaryen.f64;
if (isPrimitiveId(type, "void")) return binaryen.none;
if (type.isObjectType()) return buildObjectType(opts, type);
if (type.isUnionType()) return buildUnionType(opts, type);
if (type.isDsArrayType()) return buildDsArrayType(opts, type);
throw new Error(`Unsupported type ${type}`);
};
Expand All @@ -461,6 +476,18 @@ const buildDsArrayType = (opts: CompileExprOpts, type: DsArrayType) => {
return type.binaryenType;
};

const buildUnionType = (opts: MapBinTypeOpts, union: UnionType): TypeRef => {
if (union.hasAttribute("binaryenType")) {
return union.getAttribute("binaryenType") as TypeRef;
}

union.types.forEach((type) => mapBinaryenType(opts, type));

const typeRef = mapBinaryenType(opts, voidBaseObject);
union.setAttribute("binaryenType", typeRef);
return typeRef;
};

// Marks the start of the fields in an object after RTT info fields
const OBJECT_FIELDS_OFFSET = 2;

Expand Down
46 changes: 46 additions & 0 deletions src/parser/__tests__/__snapshots__/parser.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,52 @@ exports[`parser can parse a file into a syntax expanded ast 1`] = `
],
],
],
[
"match",
"x",
[
":",
[
"Some",
[
"generics",
"i32",
],
],
[
"Some",
[
"generics",
"i32",
],
[
"object",
[
":",
"value",
[
"+",
[
"value",
"x",
],
1,
],
],
],
],
],
[
":",
"None",
[
"None",
[
"object",
],
],
],
],
[
"fn",
[
Expand Down
4 changes: 4 additions & 0 deletions src/parser/__tests__/fixtures/void-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ obj Test<T> {
c: i32
}

match(x)
Some<i32>: Some<i32> { value: x.value + 1 }
None: None {}

fn test<T>(a: 1) -> i32

fn main()
Expand Down
5 changes: 4 additions & 1 deletion src/parser/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ export const lexer = (chars: CharStream): Token => {

const consumeOperator = (chars: CharStream, token: Token) => {
while (isOpChar(chars.next)) {
if (token.value === ">" && chars.next === ">") break; // Ugly hack to support generics, means >> is not a valid operator
if (token.value === ">" && (chars.next === ">" || chars.next === ":")) {
break; // Ugly hack to support generics, means >> is not a valid operator
}

token.addChar(chars.consumeChar());
}
};
Expand Down
Loading