From 9bbe1a3f7de90cb730df721fe146f4f874b59542 Mon Sep 17 00:00:00 2001 From: Reuben Thomas Date: Tue, 28 Nov 2023 17:51:25 +0000 Subject: [PATCH] Match update to grammar for break/continue/return --- src/compiler.ts | 49 +++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/src/compiler.ts b/src/compiler.ts index d3b3f90..c722683 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -173,12 +173,6 @@ semantics.addOperation('toAST(env,lval,inLoop,inFn)', { return addLoc(compiledSeq, this) }, - PrimaryExp_continue(_continue) { - if (!this.args.inLoop) { - throw new UrsaCompilerError(_continue.source, 'continue used outside a loop') - } - return addLoc(new ArkCall(new ArkLiteral(intrinsics.get('continue')!), []), this) - }, PrimaryExp_ident(_sym) { const symref = this.symref(this.args.env).value return addLoc(this.args.lval ? symref : new ArkGet(symref), this) @@ -361,24 +355,6 @@ semantics.addOperation('toAST(env,lval,inLoop,inFn)', { return new ArkLet(['_for'], letBody) }, - UnaryExp_break(_break, exp) { - if (!this.args.inLoop) { - throw new UrsaCompilerError(_break.source, 'break used outside a loop') - } - return addLoc(new ArkCall( - new ArkLiteral(intrinsics.get('break')!), - [maybeVal(this.args.env, exp, this.args.inFn)], - ), this) - }, - UnaryExp_return(_return, exp) { - if (!this.args.inFn) { - throw new UrsaCompilerError(_return.source, 'return used outside a function') - } - return addLoc(new ArkCall( - new ArkLiteral(intrinsics.get('return')!), - [maybeVal(this.args.env, exp, this.args.inFn)], - ), this) - }, UnaryExp_not(_not, exp) { return addLoc(new ArkCall( new ArkLiteral(intrinsics.get('not')!), @@ -603,6 +579,31 @@ semantics.addOperation('toAST(env,lval,inLoop,inFn)', { return addLoc(compiled, this) }, + Exp_break(_break, exp) { + if (!this.args.inLoop) { + throw new UrsaCompilerError(_break.source, 'break used outside a loop') + } + return addLoc(new ArkCall( + new ArkLiteral(intrinsics.get('break')!), + [maybeVal(this.args.env, exp, this.args.inFn)], + ), this) + }, + Exp_continue(_continue) { + if (!this.args.inLoop) { + throw new UrsaCompilerError(_continue.source, 'continue used outside a loop') + } + return addLoc(new ArkCall(new ArkLiteral(intrinsics.get('continue')!), []), this) + }, + Exp_return(_return, exp) { + if (!this.args.inFn) { + throw new UrsaCompilerError(_return.source, 'return used outside a function') + } + return addLoc(new ArkCall( + new ArkLiteral(intrinsics.get('return')!), + [maybeVal(this.args.env, exp, this.args.inFn)], + ), this) + }, + Lets(lets) { const parsedLets = [] const letIds: string[] = []