Skip to content

Commit

Permalink
Merge branch 'main' into lift-rework
Browse files Browse the repository at this point in the history
  • Loading branch information
AjaniBilby committed Mar 27, 2024
2 parents d130665 + 3a318cb commit 3c13816
Show file tree
Hide file tree
Showing 37 changed files with 1,245 additions and 683 deletions.
5 changes: 2 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
"files.associations": {},
"editor.insertSpaces": false,
"files.eol": "\n",
"editor.rulers": [ 80, 120 ],
"cSpell.words": [
"bitcode",
"codegen",
"Fuwawa",
"impls",
"iovs",
"Yeet"
"iovs"
],
"deno.enable": true
}
33 changes: 23 additions & 10 deletions source/bnf/syntax.bnf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
program ::= %w* ( stmt_top %w* )* ;
stmt_top ::= function | structure ;
stmt_top ::= function | structure | external ;


#=============================
Expand Down Expand Up @@ -32,8 +32,10 @@ constant ::= boolean
| string
| float | integer ;

string ::= string_text ;
string_text ::= %"\'" ( ( "\\" !"" ) | !( "\'" ) )* %"\'" ;
string ::= string_ascii | string_utf8 ;
string_ascii ::= %"\'" ( str_escape | !( "\'" ) )* %"\'" ;
string_utf8 ::= %"\"" ( str_escape | !( "\"" ) )* %"\"" ;
str_escape ::= %"\\" !"" ;

boolean ::= "true" | "false" ;

Expand Down Expand Up @@ -80,7 +82,7 @@ container ::= %(w* "[" w*) ( container_item ( %(w* "," w*) container_item )* %w*
#=============================
# Function
#=============================
function ::= func_head %w* ( block | ";" ) ;
function ::= func_head %w* ( block | ";" ) %w* ;
func_head ::= %("fn" w+) ...name %( w* "(" w* ) func_args %(w* ")" w*) %(":" w*) access ;
func_args ::= ( func_arg %w* ( %( "," w* ) func_arg )* )? ;
func_arg ::= ...name %( w* ":" w* ) access ;
Expand All @@ -91,10 +93,11 @@ block ::= %( "{" w* ) block_stmt* %( w* "}" w* ) ;
func_call ::= access func_call_body;
func_call_body ::= %( w* "(" w* ) ( expr %w* ( %( "," w* ) expr %w* )* )? %( ")" w* ) ;

return ::= %"return" "_call"? %w+ expr? %( ";" w* );
return ::= %"return" "_tail"? ( %w+ expr)? %( w* ";" w* );
raise ::= %"raise" %w+ expr %( ";" w* ); # TODO rename to lift
# drop ::= %"drop" %w+ expr %( ";" w* );


#=============================
# Expression
#=============================
Expand All @@ -105,16 +108,26 @@ expr ::= expr_arg %w* ( ...expr_infix %w* expr_arg %w* )* ;
| "as" | "instanceof"
| "." | "->" ;
expr_postfix ::= expr_call | expr_get | expr_param | expr_loan ;
expr_param ::= %"#[" %w* arg_list %w* %"]" ;
expr_call ::= %"(" %w* arg_list %w* %")" ;
expr_get ::= %"[" %w* arg_list %w* %"]" ;
expr_param ::= %"#[" %w* arg_list? %w* %"]" ;
expr_call ::= %"(" %w* arg_list? %w* %")" ;
expr_get ::= %"[" %w* arg_list? %w* %"]" ;
expr_loan ::= "@" | "$" ;
expr_arg ::= expr_prefix? %w* expr_val %w* expr_postfix* ;
expr_val ::= constant | expr_brackets | block | container | if | name ;
expr_brackets ::= %( "(" w* ) expr %( w* ")" ) ;

arg_list ::= ( expr %w* ","? %w* )* ;
arg_list ::= expr ( %(w* "," w*) expr )* ;

if ::= %("if" w*) expr %w* expr %w* ( %"else" %w* expr )? ;

statement ::= expr %terminate ;
statement ::= expr %terminate ;


#=============================
# External
#=============================
external ::= %( "external" w+ ) ( ext_import | ext_export ) ;
ext_import ::= %( "import" w* "{" w* ) ext_imports* %( w* "}" w* "from" w*) string %(w* ";" w*) ;
ext_imports ::= function | ext_import_var ;
ext_import_var ::= %( "let" w* ) name %( w* ":" w* ) access %(w* ";" w*);
ext_export ::= "export" ;
156 changes: 138 additions & 18 deletions source/bnf/syntax.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export type Term_Stmt_top = {
count: number,
ref: _Shared.ReferenceRange,
value: [
(Term_Function | Term_Structure)
(Term_Function | Term_Structure | Term_External)
]
}
export declare function Parse_Stmt_top (i: string, refMapping?: boolean): _Shared.ParseError | {
Expand Down Expand Up @@ -235,7 +235,7 @@ export type Term_String = {
count: number,
ref: _Shared.ReferenceRange,
value: [
Term_String_text
(Term_String_ascii | Term_String_utf8)
]
}
export declare function Parse_String (i: string, refMapping?: boolean): _Shared.ParseError | {
Expand All @@ -245,28 +245,52 @@ export declare function Parse_String (i: string, refMapping?: boolean): _Shared.
isPartial: boolean
}

export type Term_String_text = {
type: 'string_text',
export type Term_String_ascii = {
type: 'string_ascii',
start: number,
end: number,
count: number,
ref: _Shared.ReferenceRange,
value: [
{ type: '(...)*', value: Array<({
type: '(...)',
{ type: '(...)*', value: Array<(Term_Str_escape | _Literal)>, start: number, end: number, count: number, ref: _Shared.ReferenceRange }
]
}
export declare function Parse_String_ascii (i: string, refMapping?: boolean): _Shared.ParseError | {
root: _Shared.SyntaxNode & Term_String_ascii,
reachBytes: number,
reach: null | _Shared.Reference,
isPartial: boolean
}

export type Term_String_utf8 = {
type: 'string_utf8',
start: number,
end: number,
count: number,
ref: _Shared.ReferenceRange,
value: [
_Literal & {value: "\x5c"},
_Literal
{ type: '(...)*', value: Array<(Term_Str_escape | _Literal)>, start: number, end: number, count: number, ref: _Shared.ReferenceRange }
]
} | _Literal)>, start: number, end: number, count: number, ref: _Shared.ReferenceRange }
}
export declare function Parse_String_utf8 (i: string, refMapping?: boolean): _Shared.ParseError | {
root: _Shared.SyntaxNode & Term_String_utf8,
reachBytes: number,
reach: null | _Shared.Reference,
isPartial: boolean
}

export type Term_Str_escape = {
type: 'str_escape',
start: number,
end: number,
count: number,
ref: _Shared.ReferenceRange,
value: [
_Literal
]
}
export declare function Parse_String_text (i: string, refMapping?: boolean): _Shared.ParseError | {
root: _Shared.SyntaxNode & Term_String_text,
export declare function Parse_Str_escape (i: string, refMapping?: boolean): _Shared.ParseError | {
root: _Shared.SyntaxNode & Term_Str_escape,
reachBytes: number,
reach: null | _Shared.Reference,
isPartial: boolean
Expand Down Expand Up @@ -913,8 +937,17 @@ export type Term_Return = {
count: number,
ref: _Shared.ReferenceRange,
value: [
{ type: '(...)?', value: [] | [_Literal & {value: "\x5fcall"}], start: number, end: number, count: number, ref: _Shared.ReferenceRange },
{ type: '(...)?', value: [] | [Term_Expr], start: number, end: number, count: number, ref: _Shared.ReferenceRange }
{ type: '(...)?', value: [] | [_Literal & {value: "\x5ftail"}], start: number, end: number, count: number, ref: _Shared.ReferenceRange },
{ type: '(...)?', value: [] | [{
type: '(...)',
start: number,
end: number,
count: number,
ref: _Shared.ReferenceRange,
value: [
Term_Expr
]
}], start: number, end: number, count: number, ref: _Shared.ReferenceRange }
]
}
export declare function Parse_Return (i: string, refMapping?: boolean): _Shared.ParseError | {
Expand Down Expand Up @@ -1027,7 +1060,7 @@ export type Term_Expr_param = {
count: number,
ref: _Shared.ReferenceRange,
value: [
Term_Arg_list
{ type: '(...)?', value: [] | [Term_Arg_list], start: number, end: number, count: number, ref: _Shared.ReferenceRange }
]
}
export declare function Parse_Expr_param (i: string, refMapping?: boolean): _Shared.ParseError | {
Expand All @@ -1044,7 +1077,7 @@ export type Term_Expr_call = {
count: number,
ref: _Shared.ReferenceRange,
value: [
Term_Arg_list
{ type: '(...)?', value: [] | [Term_Arg_list], start: number, end: number, count: number, ref: _Shared.ReferenceRange }
]
}
export declare function Parse_Expr_call (i: string, refMapping?: boolean): _Shared.ParseError | {
Expand All @@ -1061,7 +1094,7 @@ export type Term_Expr_get = {
count: number,
ref: _Shared.ReferenceRange,
value: [
Term_Arg_list
{ type: '(...)?', value: [] | [Term_Arg_list], start: number, end: number, count: number, ref: _Shared.ReferenceRange }
]
}
export declare function Parse_Expr_get (i: string, refMapping?: boolean): _Shared.ParseError | {
Expand Down Expand Up @@ -1148,15 +1181,15 @@ export type Term_Arg_list = {
count: number,
ref: _Shared.ReferenceRange,
value: [
Term_Expr,
{ type: '(...)*', value: Array<{
type: '(...)',
start: number,
end: number,
count: number,
ref: _Shared.ReferenceRange,
value: [
Term_Expr,
{ type: '(...)?', value: [] | [_Literal & {value: "\x2c"}], start: number, end: number, count: number, ref: _Shared.ReferenceRange }
Term_Expr
]
}>, start: number, end: number, count: number, ref: _Shared.ReferenceRange }
]
Expand Down Expand Up @@ -1212,3 +1245,90 @@ export declare function Parse_Statement (i: string, refMapping?: boolean): _Shar
reach: null | _Shared.Reference,
isPartial: boolean
}

export type Term_External = {
type: 'external',
start: number,
end: number,
count: number,
ref: _Shared.ReferenceRange,
value: [
(Term_Ext_import | Term_Ext_export)
]
}
export declare function Parse_External (i: string, refMapping?: boolean): _Shared.ParseError | {
root: _Shared.SyntaxNode & Term_External,
reachBytes: number,
reach: null | _Shared.Reference,
isPartial: boolean
}

export type Term_Ext_import = {
type: 'ext_import',
start: number,
end: number,
count: number,
ref: _Shared.ReferenceRange,
value: [
{ type: '(...)*', value: Array<Term_Ext_imports>, start: number, end: number, count: number, ref: _Shared.ReferenceRange },
Term_String
]
}
export declare function Parse_Ext_import (i: string, refMapping?: boolean): _Shared.ParseError | {
root: _Shared.SyntaxNode & Term_Ext_import,
reachBytes: number,
reach: null | _Shared.Reference,
isPartial: boolean
}

export type Term_Ext_imports = {
type: 'ext_imports',
start: number,
end: number,
count: number,
ref: _Shared.ReferenceRange,
value: [
(Term_Function | Term_Ext_import_var)
]
}
export declare function Parse_Ext_imports (i: string, refMapping?: boolean): _Shared.ParseError | {
root: _Shared.SyntaxNode & Term_Ext_imports,
reachBytes: number,
reach: null | _Shared.Reference,
isPartial: boolean
}

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

export type Term_Ext_export = {
type: 'ext_export',
start: number,
end: number,
count: number,
ref: _Shared.ReferenceRange,
value: [
_Literal & {value: "export"}
]
}
export declare function Parse_Ext_export (i: string, refMapping?: boolean): _Shared.ParseError | {
root: _Shared.SyntaxNode & Term_Ext_export,
reachBytes: number,
reach: null | _Shared.Reference,
isPartial: boolean
}
27 changes: 24 additions & 3 deletions source/bnf/syntax.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions source/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Function from "~/compiler/function.ts";
import Package from "~/compiler/package.ts";
import Project from "~/compiler/project.ts";
import { DisplayTimers, TimerStart, TimerEnd } from "~/helper.ts";
import { Panic } from "~/helper.ts";
import { Panic } from "~/compiler/helper.ts";

if (Deno.args.includes("--version")) {
console.log("version: 0.0.0");
Expand Down Expand Up @@ -54,7 +54,7 @@ TimerEnd("serialize");
TimerStart("wasm2wat");
const command = new Deno.Command(
"wasm2wat",
{ args: ["-v", "out.wasm", "-o", "out.wat"] }
{ args: ["-v", "out.wasm", "-o", "out.wat", "--enable-all"] }
);
const { code, stdout, stderr } = await command.output();
if (code !== 0) {
Expand Down
5 changes: 3 additions & 2 deletions source/compiler/codegen/allocation/stack.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AssertUnreachable, AlignUpInteger, AlignDownInteger, LatentValue } from "~/helper.ts";
import { AlignUpInteger, AlignDownInteger } from "~/compiler/helper.ts";
import { AssertUnreachable, LatentValue } from "~/helper.ts";

/**
* Used for calculating the relative stack location of variables within a function stack
Expand Down Expand Up @@ -171,7 +172,7 @@ export class StackAllocator {
}

resolve() {
if (this.checkpointRef.hasAllocations()) throw new Error(
if (this.checkpointRef.hasAllocations()) console.warn(
`Stack leak: ${this.checkpointRef.getAllocationCount()} stack values are still allocated after stack frame end `
+ this.checkpointRef.getAllocations()
);
Expand Down
Loading

0 comments on commit 3c13816

Please sign in to comment.