parse annos
This commit is contained in:
parent
072e44eaa8
commit
4a9d501a71
@ -39,7 +39,7 @@ export type StmtKind =
|
||||
params: Param[];
|
||||
returnType?: EType;
|
||||
body: Expr;
|
||||
annos?: Anno[];
|
||||
anno?: Anno;
|
||||
vtype?: VType;
|
||||
}
|
||||
| { type: "let"; param: Param; value: Expr }
|
||||
|
@ -14,9 +14,12 @@ const lexer = new Lexer(text, reporter);
|
||||
|
||||
const parser = new Parser(lexer, reporter);
|
||||
const ast = parser.parseStmts();
|
||||
|
||||
console.log(JSON.stringify(ast, null, 4));
|
||||
|
||||
/*
|
||||
new Resolver(reporter).resolve(ast);
|
||||
new Checker(reporter).check(ast);
|
||||
// console.log(JSON.stringify(ast, null, 4))
|
||||
|
||||
if (reporter.errorOccured()) {
|
||||
console.error("Errors occurred, stopping compilation.");
|
||||
@ -31,3 +34,4 @@ const program = lowerer.finish();
|
||||
console.log(JSON.stringify(program));
|
||||
|
||||
await Deno.writeTextFile("out.slgbc", JSON.stringify(program));
|
||||
*/
|
||||
|
@ -205,19 +205,26 @@ export class Parser {
|
||||
returnType = this.parseEType();
|
||||
}
|
||||
|
||||
let annos: Anno[] | null = null;
|
||||
let anno: Anno | null = null;
|
||||
if (this.test("#")) {
|
||||
annos = this.parseAnnoArgs();
|
||||
anno = this.parseAnno();
|
||||
}
|
||||
if (!this.test("{")) {
|
||||
this.report("expected block");
|
||||
return this.stmt({ type: "error" }, pos);
|
||||
}
|
||||
const body = this.parseBlock();
|
||||
if (returnType === null) {
|
||||
return this.stmt({ type: "fn", ident, params, body }, pos);
|
||||
}
|
||||
return this.stmt({ type: "fn", ident, params, returnType, body }, pos);
|
||||
return this.stmt(
|
||||
{
|
||||
type: "fn",
|
||||
ident,
|
||||
params,
|
||||
returnType: returnType !== null ? returnType : undefined,
|
||||
body,
|
||||
anno: anno != null ? anno : undefined,
|
||||
},
|
||||
pos,
|
||||
);
|
||||
}
|
||||
|
||||
public parseAnnoArgs(): Expr[] {
|
||||
@ -246,34 +253,26 @@ export class Parser {
|
||||
return annoArgs;
|
||||
}
|
||||
|
||||
public parseAnnos(): Anno[] {
|
||||
public parseAnno(): Anno | null {
|
||||
const pos = this.pos();
|
||||
this.step();
|
||||
if (!this.test("[")) {
|
||||
this.report("expected '['");
|
||||
return [];
|
||||
return null;
|
||||
}
|
||||
this.step();
|
||||
const annoArgs: Expr[] = [];
|
||||
if (!this.test(")")) {
|
||||
if (!this.test("ident")) {
|
||||
this.report("expected identifier");
|
||||
return [];
|
||||
}
|
||||
annoArgs.push(this.parseExpr());
|
||||
while (this.test(",")) {
|
||||
this.step();
|
||||
if (this.test(")")) {
|
||||
break;
|
||||
}
|
||||
annoArgs.push(this.parseExpr());
|
||||
}
|
||||
if (!this.test("ident")) {
|
||||
this.report("expected identifier");
|
||||
return null;
|
||||
}
|
||||
if (!this.test(")")) {
|
||||
this.report("expected ')'");
|
||||
return [];
|
||||
const ident = this.current().identValue!;
|
||||
const values = this.parseAnnoArgs();
|
||||
if (!this.test("]")) {
|
||||
this.report("expected ']'");
|
||||
return null;
|
||||
}
|
||||
this.step();
|
||||
return annoArgs;
|
||||
return { ident, pos, values };
|
||||
}
|
||||
|
||||
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