Skip to content

Commit

Permalink
~Basic reassignment
Browse files Browse the repository at this point in the history
  • Loading branch information
AjaniBilby committed Sep 18, 2023
1 parent 12e26dc commit 465b6de
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 11 deletions.
3 changes: 2 additions & 1 deletion source/bnf/syntax.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ access ::= name ( %w* accessor )* ;
access_comp ::= %"#[]";

declare ::= %( "let" w* ) name %w* (%":" %w* access %w*)? %("=" w*) expr %(w* ";") ;
assign ::= name %w* %("=" w*) expr %(w* ";") ;



Expand All @@ -69,7 +70,7 @@ function ::= func_head %w* ( func_body | ";" ) ;
func_args ::= ( func_arg %w* ( %( "," w* ) func_arg )* )? ;
func_arg ::= ...name %( w* ":" w* ) access ;
func_body ::= %( "{" w* ) ( func_stmt %w* )* %( w* "}" w* ";"? ) ;
func_stmt ::= declare | statement | return ;
func_stmt ::= declare | assign | statement | return ;

func_call ::= access func_call_body;
func_call_body ::= %( w* "(" w* ) ( expr %w* ( %( "," w* ) expr %w* )* )? %( ")" w* ) ;
Expand Down
22 changes: 20 additions & 2 deletions source/bnf/syntax.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type * as _Shared from './shared.js';
import type _Shared from './shared.js';
export type _Literal = { type: "literal", value: string, start: number, end: number, count: number, ref: _Shared.ReferenceRange };
export type Term_Program = {
type: 'program',
Expand Down Expand Up @@ -496,6 +496,24 @@ export declare function Parse_Declare (i: string, refMapping?: boolean): _Shared
isPartial: boolean
}

export type Term_Assign = {
type: 'assign',
start: number,
end: number,
count: number,
ref: _Shared.ReferenceRange,
value: [
Term_Name,
Term_Expr
]
}
export declare function Parse_Assign (i: string, refMapping?: boolean): _Shared.ParseError | {
root: _Shared.SyntaxNode & Term_Assign,
reachBytes: number,
reach: null | _Shared.Reference,
isPartial: boolean
}

export type Term_Function = {
type: 'function',
start: number,
Expand Down Expand Up @@ -620,7 +638,7 @@ export type Term_Func_stmt = {
count: number,
ref: _Shared.ReferenceRange,
value: [
(Term_Declare | Term_Statement | Term_Return)
(Term_Declare | Term_Assign | Term_Statement | Term_Return)
]
}
export declare function Parse_Func_stmt (i: string, refMapping?: boolean): _Shared.ParseError | {
Expand Down
5 changes: 4 additions & 1 deletion source/bnf/syntax.js

Large diffs are not rendered by default.

44 changes: 37 additions & 7 deletions source/compiler/codegen/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import type { Scope } from "./scope.ts";

import * as banned from "./banned.ts";
import { Instruction, AnyInstruction } from "../../wasm/index.ts";
import { AssertUnreachable } from "../../bnf/shared.js";
import { CompileExpr } from "./expression/index.ts";
import { Intrinsic } from "../intrinsic.ts";
import { Yeet } from "../../helper.ts";
import { Intrinsic, i16, i8, u16, u8 } from "../intrinsic.ts";
import { AssertUnreachable, Yeet } from "../../helper.ts";

export class Context {
file: File;
Expand All @@ -32,6 +31,7 @@ export class Context {

switch (line.type) {
case "declare": CompileDeclare (this, line); break;
case "assign": CompileAssign (this, line); break;
case "statement": CompileExprStmt (this, line); break;
case "return": CompileReturn (this, line); break;
default: AssertUnreachable(line);
Expand Down Expand Up @@ -63,6 +63,13 @@ function CompileDeclare(ctx: Context, syntax: Syntax.Term_Declare) {
name: ctx.file.name,
ref: type.ref
})

if (typeRef === i8 || typeRef === u8 || typeRef === i16 || typeRef === u16)
Yeet(`${colors.red("Error")}: Cannot explicitly use virtual integer types\n`, {
path: ctx.file.path,
name: ctx.file.name,
ref: type.ref
})
}

const resolveType: Intrinsic = CompileExpr(ctx, value, typeRef || undefined);
Expand All @@ -74,21 +81,44 @@ function CompileDeclare(ctx: Context, syntax: Syntax.Term_Declare) {
})

if (typeRef && resolveType !== typeRef)
Yeet(`${colors.red("Error")}: type ${resolveType.name} != type ${typeRef.name}\n`, {
Yeet(`${colors.red("Error")}: type ${typeRef.name} != type ${resolveType.name}\n`, {
path: ctx.file.path,
name: ctx.file.name,
ref: type?.ref || syntax.ref
})

const reg = ctx.scope.registerVariable(name, typeRef || resolveType, syntax.ref);
if (!reg)
const variable = ctx.scope.registerVariable(name, typeRef || resolveType, syntax.ref);
if (!variable)
Yeet(`${colors.red("Error")}: Variable ${name} is already declared\n`, {
path: ctx.file.path,
name: ctx.file.name,
ref: syntax.ref
})

ctx.block.push(Instruction.local.set(reg.register.ref));
ctx.block.push(Instruction.local.set(variable.register.ref));
}

function CompileAssign(ctx: Context, syntax: Syntax.Term_Assign) {
const name = syntax.value[0].value[0].value;
const value = syntax.value[1];

const variable = ctx.scope.getVariable(name);
if (!variable)
Yeet(`${colors.red("Error")}: Undeclared variable ${name}\n`, {
path: ctx.file.path,
name: ctx.file.name,
ref: syntax.ref
})

const resolveType: Intrinsic = CompileExpr(ctx, value, variable.type);
if (resolveType !== variable.type)
Yeet(`${colors.red("Error")}: type ${variable.name} != type ${resolveType.name}\n`, {
path: ctx.file.path,
name: ctx.file.name,
ref: syntax.ref
})

ctx.block.push(Instruction.local.set(variable.register.ref));
}


Expand Down
4 changes: 4 additions & 0 deletions source/compiler/codegen/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ export class Scope {

return this.vars[name];
}

getVariable(name: string) {
return this.vars[name] || null;
}
}

0 comments on commit 465b6de

Please sign in to comment.