From 4fa0c79e265ef129aead21d9d8b4d571800576ba Mon Sep 17 00:00:00 2001 From: Jordi Baylina Date: Wed, 24 Oct 2018 19:59:50 +0200 Subject: [PATCH] If without else --- src/compiler.js | 2 +- src/exec.js | 6 ++++-- src/gencode.js | 10 +++++++--- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/compiler.js b/src/compiler.js index 9b12507..ddbba6b 100644 --- a/src/compiler.js +++ b/src/compiler.js @@ -31,7 +31,7 @@ module.exports = compile; const parser = require("../parser/jaz.js").parser; -const timeout = ms => new Promise(res => setTimeout(res, ms)) +const timeout = ms => new Promise(res => setTimeout(res, ms)); async function compile(srcFile) { const fullFileName = srcFile; diff --git a/src/exec.js b/src/exec.js index ecca341..bb85621 100644 --- a/src/exec.js +++ b/src/exec.js @@ -578,8 +578,10 @@ function execIf(ctx, ast) { exec(ctx, ast.then); if (ctx.error) return; } else { - exec(ctx, ast.else); - if (ctx.error) return; + if (ast.else) { + exec(ctx, ast.else); + if (ctx.error) return; + } } } diff --git a/src/gencode.js b/src/gencode.js index 3fbe8e0..0c3b5c2 100644 --- a/src/gencode.js +++ b/src/gencode.js @@ -246,9 +246,13 @@ function genIf(ctx, ast) { if (ctx.error) return; const thenBody = gen(ctx, ast.then); if (ctx.error) return; - const elseBody = gen(ctx, ast.else); - if (ctx.error) return; - return `if (${condition}) {\n${thenBody}\n} else {\n${elseBody}\n}\n`; + if (ast.else) { + const elseBody = gen(ctx, ast.else); + if (ctx.error) return; + return `if (${condition}) {\n${thenBody}\n} else {\n${elseBody}\n}\n`; + } else { + return `if (${condition}) {\n${thenBody}\n}\n`; + } }