mirror of
https://git.sfja.dk/Mikkel/slige.git
synced 2025-01-18 19:16:35 +00:00
parse annos
This commit is contained in:
parent
072e44eaa8
commit
4a9d501a71
@ -39,7 +39,7 @@ export type StmtKind =
|
|||||||
params: Param[];
|
params: Param[];
|
||||||
returnType?: EType;
|
returnType?: EType;
|
||||||
body: Expr;
|
body: Expr;
|
||||||
annos?: Anno[];
|
anno?: Anno;
|
||||||
vtype?: VType;
|
vtype?: VType;
|
||||||
}
|
}
|
||||||
| { type: "let"; param: Param; value: Expr }
|
| { type: "let"; param: Param; value: Expr }
|
||||||
|
@ -14,9 +14,12 @@ const lexer = new Lexer(text, reporter);
|
|||||||
|
|
||||||
const parser = new Parser(lexer, reporter);
|
const parser = new Parser(lexer, reporter);
|
||||||
const ast = parser.parseStmts();
|
const ast = parser.parseStmts();
|
||||||
|
|
||||||
|
console.log(JSON.stringify(ast, null, 4));
|
||||||
|
|
||||||
|
/*
|
||||||
new Resolver(reporter).resolve(ast);
|
new Resolver(reporter).resolve(ast);
|
||||||
new Checker(reporter).check(ast);
|
new Checker(reporter).check(ast);
|
||||||
// console.log(JSON.stringify(ast, null, 4))
|
|
||||||
|
|
||||||
if (reporter.errorOccured()) {
|
if (reporter.errorOccured()) {
|
||||||
console.error("Errors occurred, stopping compilation.");
|
console.error("Errors occurred, stopping compilation.");
|
||||||
@ -31,3 +34,4 @@ const program = lowerer.finish();
|
|||||||
console.log(JSON.stringify(program));
|
console.log(JSON.stringify(program));
|
||||||
|
|
||||||
await Deno.writeTextFile("out.slgbc", JSON.stringify(program));
|
await Deno.writeTextFile("out.slgbc", JSON.stringify(program));
|
||||||
|
*/
|
||||||
|
@ -205,19 +205,26 @@ export class Parser {
|
|||||||
returnType = this.parseEType();
|
returnType = this.parseEType();
|
||||||
}
|
}
|
||||||
|
|
||||||
let annos: Anno[] | null = null;
|
let anno: Anno | null = null;
|
||||||
if (this.test("#")) {
|
if (this.test("#")) {
|
||||||
annos = this.parseAnnoArgs();
|
anno = this.parseAnno();
|
||||||
}
|
}
|
||||||
if (!this.test("{")) {
|
if (!this.test("{")) {
|
||||||
this.report("expected block");
|
this.report("expected block");
|
||||||
return this.stmt({ type: "error" }, pos);
|
return this.stmt({ type: "error" }, pos);
|
||||||
}
|
}
|
||||||
const body = this.parseBlock();
|
const body = this.parseBlock();
|
||||||
if (returnType === null) {
|
return this.stmt(
|
||||||
return this.stmt({ type: "fn", ident, params, body }, pos);
|
{
|
||||||
}
|
type: "fn",
|
||||||
return this.stmt({ type: "fn", ident, params, returnType, body }, pos);
|
ident,
|
||||||
|
params,
|
||||||
|
returnType: returnType !== null ? returnType : undefined,
|
||||||
|
body,
|
||||||
|
anno: anno != null ? anno : undefined,
|
||||||
|
},
|
||||||
|
pos,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public parseAnnoArgs(): Expr[] {
|
public parseAnnoArgs(): Expr[] {
|
||||||
@ -246,34 +253,26 @@ export class Parser {
|
|||||||
return annoArgs;
|
return annoArgs;
|
||||||
}
|
}
|
||||||
|
|
||||||
public parseAnnos(): Anno[] {
|
public parseAnno(): Anno | null {
|
||||||
|
const pos = this.pos();
|
||||||
this.step();
|
this.step();
|
||||||
if (!this.test("[")) {
|
if (!this.test("[")) {
|
||||||
this.report("expected '['");
|
this.report("expected '['");
|
||||||
return [];
|
return null;
|
||||||
}
|
}
|
||||||
this.step();
|
this.step();
|
||||||
const annoArgs: Expr[] = [];
|
|
||||||
if (!this.test(")")) {
|
|
||||||
if (!this.test("ident")) {
|
if (!this.test("ident")) {
|
||||||
this.report("expected identifier");
|
this.report("expected identifier");
|
||||||
return [];
|
return null;
|
||||||
}
|
}
|
||||||
annoArgs.push(this.parseExpr());
|
const ident = this.current().identValue!;
|
||||||
while (this.test(",")) {
|
const values = this.parseAnnoArgs();
|
||||||
this.step();
|
if (!this.test("]")) {
|
||||||
if (this.test(")")) {
|
this.report("expected ']'");
|
||||||
break;
|
return null;
|
||||||
}
|
|
||||||
annoArgs.push(this.parseExpr());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!this.test(")")) {
|
|
||||||
this.report("expected ')'");
|
|
||||||
return [];
|
|
||||||
}
|
}
|
||||||
this.step();
|
this.step();
|
||||||
return annoArgs;
|
return { ident, pos, values };
|
||||||
}
|
}
|
||||||
|
|
||||||
public parseFnParams(): Param[] {
|
public parseFnParams(): Param[] {
|
||||||
|
5
examples/annos.slg
Normal file
5
examples/annos.slg
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
fn print(msg: string) #[builtin(print)] {}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
print("hello world!");
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user