Skip to content

Commit

Permalink
read module name from syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
AjaniBilby committed Mar 23, 2024
1 parent 537cf4e commit c178c6e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 22 deletions.
5 changes: 3 additions & 2 deletions source/bnf/syntax.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ constant ::= boolean
| float | integer ;

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

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

Expand Down
33 changes: 15 additions & 18 deletions source/bnf/syntax.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,17 +252,7 @@ export type Term_String_ascii = {
count: number,
ref: _Shared.ReferenceRange,
value: [
{ type: '(...)*', value: Array<({
type: '(...)',
start: number,
end: number,
count: number,
ref: _Shared.ReferenceRange,
value: [
_Literal & {value: "\x5c"},
_Literal
]
} | _Literal)>, start: number, end: number, count: number, ref: _Shared.ReferenceRange }
{ 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 | {
Expand All @@ -279,21 +269,28 @@ export type Term_String_utf8 = {
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_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 & {value: "\x5c"},
_Literal
]
} | _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,
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
5 changes: 4 additions & 1 deletion source/bnf/syntax.js

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions source/compiler/codegen/expression/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,29 @@ function CompileFloat(ctx: Context, syntax: Syntax.Term_Float, expect?: Intrinsi
ctx.block.push(Instruction.const.f32(num));

return f32.value;
}

export function SimplifyString(syntax: Syntax.Term_String) {
const inner = syntax.value[0];
const type = inner.type === "string_ascii" ? "ascii" : "utf8";
let str = "";

for (const chunk of inner.value[0].value) {
if (chunk.type == "literal") {
str += chunk.value;
continue;
}

const esc = chunk.value[0].value;
switch (esc) {
case "0": str += "\0"; break;
case "f": str += "\f"; break;
case "n": str += "\n"; break;
case "r": str += "\r"; break;
case "v": str += "\v"; break;
default: str += esc;
}
}

return { type, str }
}
4 changes: 3 additions & 1 deletion source/compiler/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Structure from "~/compiler/structure.ts";
import Function from "~/compiler/function.ts";
import Global from "~/compiler/global.ts";
import Import from "~/compiler/import.ts";
import { SimplifyString } from "~/compiler/codegen/expression/constant.ts";

export type Namespace = Function | Import | Global | Structure | IntrinsicType | VirtualType ;

Expand Down Expand Up @@ -122,12 +123,13 @@ function IngestStructure(file: File, syntax: Term_Structure) {
function IngestExternal(file: File, syntax: Term_External) {
if (syntax.value[0].type !== "ext_import") throw new Error(`Unsupported external export`);

const name = SimplifyString(syntax.value[0].value[1]);
for (const inner of syntax.value[0].value[0].value) {
const line = inner.value[0];
const type = line.type;
switch (type) {
case "function": {
IngestFunction(file, line, "ext");
IngestFunction(file, line, name.str);
} break;
case "ext_import_var": throw new Error(`Import global unimplemented`);
default: AssertUnreachable(type);
Expand Down

0 comments on commit c178c6e

Please sign in to comment.