Skip to content

Commit

Permalink
+Basic function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
AjaniBilby committed Sep 21, 2023
1 parent 4818b96 commit 3cc644f
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
5 changes: 3 additions & 2 deletions source/compiler/codegen/expression/operand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as colors from "https://deno.land/[email protected]/fmt/colors.ts";
import type * as Syntax from "../../../bnf/syntax.d.ts";
import { CompileConstFloat, CompileConstInt } from "./constant.ts";
import { AssertUnreachable, Yeet } from "../../../helper.ts";
import { CompilePostfixes } from "./postfix.ts";
import { CompilePrefix } from "./prefix.ts";
import { Instruction } from "../../../wasm/index.ts";
import { CompileExpr } from "./index.ts";
Expand All @@ -26,8 +27,8 @@ export function CompileArg(ctx: Context, syntax: Syntax.Term_Expr_arg, expect?:
default: AssertUnreachable(val);
}

if (postfix.length > 0) throw new Error("Unimplemented postfix operations");
if (prefix) return CompilePrefix(ctx, prefix, res, expect);
if (prefix) res = CompilePrefix(ctx, prefix, res, expect);
if (postfix.length > 0) CompilePostfixes(ctx, postfix, res, expect);

return res;
}
Expand Down
81 changes: 81 additions & 0 deletions source/compiler/codegen/expression/postfix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import * as colors from "https://deno.land/[email protected]/fmt/colors.ts";

import type * as Syntax from "../../../bnf/syntax.d.ts";
import Function from "../../function.ts";
import { AssertUnreachable, Yeet } from "../../../helper.ts";
import { CompileArg } from "./operand.ts";
import { Intrinsic } from "../../intrinsic.ts";
import { Namespace } from "../../file.ts";
import { Context } from "./../context.ts";
import { Instruction } from "../../../wasm/index.ts";


export type OperandType = Intrinsic | Namespace;


export function CompilePostfixes(ctx: Context, syntax: Syntax.Term_Expr_postfix[], type: OperandType, expect?: Intrinsic): OperandType {
let res = type;
for (const postfix of syntax) {
const act = postfix.value[0];

switch (act.type) {
case "expr_call": res = CompileCall(ctx, act, res); break;
case "expr_get": Yeet(
`${colors.red("Error")}: Unimplemented postfix operation ${act.type}\n`,
{ path: ctx.file.path, name: ctx.file.name, ref: act.ref }
); break;
case "expr_param": Yeet(
`${colors.red("Error")}: Unimplemented postfix operation ${act.type}\n`,
{ path: ctx.file.path, name: ctx.file.name, ref: act.ref }
); break;
default: AssertUnreachable(act);
}
}

return res;
}


function CompileCall(ctx: Context, syntax: Syntax.Term_Expr_call, operand: OperandType, expect?: Intrinsic) {
if (!(operand instanceof Function)) Yeet(
`${colors.red("Error")}: Cannot call on a non function value\n`,
{ path: ctx.file.path, name: ctx.file.name, ref: syntax.ref }
);

operand.compile(); // check the function is compiled

if (operand.returns.length != 1) Yeet(
`${colors.red("Error")}: Cannot currently handle functions which don't return a single value\n`,
{ path: ctx.file.path, name: ctx.file.name, ref: syntax.ref }
);

if (!operand.ref) throw new Error("A function somehow compiled with a reference generated");

const args = LineariseArgList(syntax.value[0]);
if (args.length != operand.arguments.length) Yeet(
`${colors.red("Error")}: Miss matched argument count\n`,
{ path: ctx.file.path, name: ctx.file.name, ref: syntax.ref }
);

for (let i=0; i<args.length; i++) {
const signature = operand.arguments[i];
const res = CompileArg(ctx, args[i], signature.type);

if (res !== signature.type) Yeet(
`${colors.red("Error")}: Call argument type miss-match, expected ${signature.type.name} got ${res.name}\n`,
{ path: ctx.file.path, name: ctx.file.name, ref: args[i].ref }
);
}

ctx.block.push(Instruction.call(operand.ref));

return operand.returns[0];
}





function LineariseArgList(args: Syntax.Term_Arg_list) {
return args.value[0].value.map(x => x.value[0]);
}

0 comments on commit 3cc644f

Please sign in to comment.