Skip to content
Open
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
121 changes: 121 additions & 0 deletions src/desugar/desugar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import type {CompilerContext} from "@/context/context";
import {getAllStaticFunctions, getAllTypes} from "@/types/resolveDescriptors";
import type * as Ast from "@/ast/ast";
import {registerExpType} from "@/types/resolveExpression";
import {binaryOperationFromAugmentedAssignOperation} from "@/ast/util";

export function desugar(ctx: CompilerContext): CompilerContext {
const funcs = getAllStaticFunctions(ctx)

for (const func of funcs) {
if (func.ast.kind === "function_def") {
func.ast = {
...func.ast,
statements: func.ast.statements.flatMap(stmt => {
if (stmt.kind === "statement_augmentedassign") {
const binaryExpression: Ast.OpBinary = {
kind: "op_binary",
loc: stmt.loc,
id: stmt.id,
left: stmt.path,
right: stmt.expression,
op: binaryOperationFromAugmentedAssignOperation(stmt.op)
}
ctx = registerExpType(ctx, binaryExpression, {
kind: "ref",
name: "Int",
optional: false,
})

return {
kind: "statement_assign",
path: stmt.path,
expression: binaryExpression,
loc: stmt.loc,
id: stmt.id,
}
}

// if (stmt.kind === "statement_assign") {
// const expr: Ast.Number = {
// kind: "number",
// base: 10,
// value: 10000n,
// id: stmt.id,
// loc: stmt.loc,
// };
// ctx = registerExpType(ctx, expr, {
// kind: "ref",
// name: "Int",
// optional: false,
// })
// return [
// stmt,
// {
// kind: "statement_return",
// expression: expr,
// loc: stmt.loc,
// id: stmt.id,
// } satisfies Ast.StatementReturn as Ast.Statement
// ]
// }
return [stmt]
})
}

}
}

const types = getAllTypes(ctx)
for (const type of types) {
type.functions.forEach((func, key) => {
if (type.name === "BaseTrait") {
return
}

if (func.ast.kind === "function_def" && func.self) {
const paramName: Ast.Id = {
kind: "id",
text: "self",
loc: func.ast.loc,
id: func.ast.id,
};

func.params.splice(0, 0, {
name: paramName,
type: {
kind: "ref",
name: func.self.kind === "ref" ? func.self.name : "",
optional: false,
},
loc: func.ast.loc,
})

func.ast = {
...func.ast,
params: [
{
kind: "typed_parameter",
name: paramName,
type: {
kind: "type_id",
text: func.self.kind === "ref" ? func.self.name : "",
loc: func.ast.loc,
id: func.ast.id,
},
loc: func.ast.loc,
id: func.ast.id,
},
...func.ast.params,
]
}

type.functions.set(key, func)
}

console.log(func.name)
})
}

return ctx;
}
1 change: 1 addition & 0 deletions src/generator/writers/writeFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ export function writeStatement(
return;
}
case "statement_augmentedassign": {
// throw new Error("unexpected statement");
const lvaluePath = tryExtractPath(f.path);
if (lvaluePath === null) {
// typechecker is supposed to catch this
Expand Down
3 changes: 3 additions & 0 deletions src/pipeline/precompile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { FactoryAst } from "@/ast/ast-helpers";
import type { Parser } from "@/grammar";
import { evalComptimeExpressions } from "@/types/evalComptimeExpressions";
import { computeReceiversEffects } from "@/types/effects";
import {desugar} from "@/desugar/desugar";

export function precompile(
ctx: CompilerContext,
Expand Down Expand Up @@ -60,6 +61,8 @@ export function precompile(
*/
evalComptimeExpressions(ctx, ast);

ctx = desugar(ctx)

// This creates TLB-style type definitions
ctx = resolveSignatures(ctx, ast);

Expand Down
2 changes: 1 addition & 1 deletion src/types/resolveExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function getExpType(ctx: CompilerContext, exp: Ast.Expression) {
return t.description;
}

function registerExpType(
export function registerExpType(
ctx: CompilerContext,
exp: Ast.Expression,
description: TypeRef,
Expand Down