From d2f3d2c8d11ecf60f356ba365e02e13e361147e4 Mon Sep 17 00:00:00 2001 From: Haroldo de Oliveira Pinheiro Date: Thu, 22 Sep 2022 19:24:30 -0300 Subject: [PATCH 1/6] Fix identation problem. --- generator/generator.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/generator/generator.js b/generator/generator.js index 8fef5cf..874225b 100644 --- a/generator/generator.js +++ b/generator/generator.js @@ -80,6 +80,8 @@ const indent = (...params) => o.flat ? o.flat() : `// Unknown value of type ${typeof o}: ${o}`) .flat() + .map(o => o.split ? o.split('\n') : o) + .flat() .map(s => '\t' + s) .join('\n'); From 681f426d5f6f3a7b29bddba571b4775078265f05 Mon Sep 17 00:00:00 2001 From: Haroldo de Oliveira Pinheiro Date: Thu, 22 Sep 2022 19:48:07 -0300 Subject: [PATCH 2/6] Implement generation of `if`/`elseif`/`endif` --- examples/test/project/startup.choice | 22 ++++++++++++++------ generator/generator.js | 30 ++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/examples/test/project/startup.choice b/examples/test/project/startup.choice index f8bd857..f314b8d 100644 --- a/examples/test/project/startup.choice +++ b/examples/test/project/startup.choice @@ -8,13 +8,23 @@ * set intVar, 3 * set localInt, intVar + 3 +* create playingMusic, false + * choice - # Play some music - * music "Actraiser - Fillmore.vgm" - OK, playing Fillmore, from Actraiser. - # Stop the music - * stop music, sound - OK, music is stopped. + * if playingMusic + # Stop the music + * stop music, sound + OK, music is stopped. + * set playingMusic, false + * elseif false + This should not appear + This should not appear, either + * else + # Play some music + * music "Actraiser - Fillmore.vgm" + OK, playing Fillmore, from Actraiser. + * set playingMusic, true + # Play a voice * sound "ready.wav" OK, playing a digital voice. diff --git a/generator/generator.js b/generator/generator.js index 874225b..73b0d9b 100644 --- a/generator/generator.js +++ b/generator/generator.js @@ -230,6 +230,36 @@ const COMMAND_GENERATORS = { } return `${existingVar.value.internalVar} = ${newValue.code};`; + }, + + 'if': (entity, context) => { + const ifCondition = getExpression(entity, entity.params.positional.condition, context, 'Condition') || {}; + const ifBody = generateFromBody(entity.body, context); + + const generatedElseIf = (entity.siblings && entity.siblings.elseif || []).map(elseIf => { + const elseIfCondition = getExpression(elseIf, elseIf.params.positional.condition, context, 'Condition') || {}; + const elseIfBody = generateFromBody(elseIf.body, context); + return [ + `} else if (${elseIfCondition.code}) {`, + indent(elseIfBody) + ].join('\n'); + }); + + const generatedElse = (entity.siblings && entity.siblings['else'] || []).map(elseEntity => { + const elseBody = generateFromBody(elseEntity.body, context); + return [ + '} else {', + indent(elseBody) + ].join('\n'); + }); + + return [ + `if (${ifCondition.code}) {`, + indent(ifBody), + generatedElseIf, + generatedElse, + '}' + ].join('\n'); } }; From d9dbb9b83bd32096b632aa95d7014fe3f9f411a4 Mon Sep 17 00:00:00 2001 From: Haroldo de Oliveira Pinheiro Date: Thu, 22 Sep 2022 20:24:00 -0300 Subject: [PATCH 3/6] Work around `else`/`elseif` bug. --- examples/test/project/startup.choice | 5 +---- parser/syntax-full.js | 4 +--- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/examples/test/project/startup.choice b/examples/test/project/startup.choice index f314b8d..003fd25 100644 --- a/examples/test/project/startup.choice +++ b/examples/test/project/startup.choice @@ -16,10 +16,7 @@ * stop music, sound OK, music is stopped. * set playingMusic, false - * elseif false - This should not appear - This should not appear, either - * else + * if !playingMusic # Play some music * music "Actraiser - Fillmore.vgm" OK, playing Fillmore, from Actraiser. diff --git a/parser/syntax-full.js b/parser/syntax-full.js index 74ec444..89961da 100644 --- a/parser/syntax-full.js +++ b/parser/syntax-full.js @@ -99,9 +99,6 @@ const checkSiblingCommands = (body, errors) => checkSiblings(checkOnlyAfter(body const completeCommands = (body, errors) => { - if (!body.map) { - console.log({ body }); - } const completedCommands = body.map(element => { if (element.type !== 'command') { return element; @@ -139,6 +136,7 @@ const parse = source => { const completedErrors = [...(errors || [])]; const completedBody = completeCommands(body, completedErrors); + debugger; return { type, From 7a1bf9988ca2315e12e2ad7a3ecfbb1397423ac6 Mon Sep 17 00:00:00 2001 From: Haroldo de Oliveira Pinheiro Date: Thu, 22 Sep 2022 20:31:24 -0300 Subject: [PATCH 4/6] Fix `else`/`elseif` generation bug. --- examples/test/project/startup.choice | 6 +++- generator/generator.js | 46 +++++++++++++++------------- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/examples/test/project/startup.choice b/examples/test/project/startup.choice index 003fd25..2994447 100644 --- a/examples/test/project/startup.choice +++ b/examples/test/project/startup.choice @@ -16,7 +16,11 @@ * stop music, sound OK, music is stopped. * set playingMusic, false - * if !playingMusic + * elseif FALSE + This should not appear + This should not play + * music "Actraiser - Fillmore.vgm" + * else # Play some music * music "Actraiser - Fillmore.vgm" OK, playing Fillmore, from Actraiser. diff --git a/generator/generator.js b/generator/generator.js index 73b0d9b..873f3f2 100644 --- a/generator/generator.js +++ b/generator/generator.js @@ -233,31 +233,33 @@ const COMMAND_GENERATORS = { }, 'if': (entity, context) => { - const ifCondition = getExpression(entity, entity.params.positional.condition, context, 'Condition') || {}; - const ifBody = generateFromBody(entity.body, context); - - const generatedElseIf = (entity.siblings && entity.siblings.elseif || []).map(elseIf => { - const elseIfCondition = getExpression(elseIf, elseIf.params.positional.condition, context, 'Condition') || {}; - const elseIfBody = generateFromBody(elseIf.body, context); - return [ - `} else if (${elseIfCondition.code}) {`, - indent(elseIfBody) - ].join('\n'); - }); + const condition = getExpression(entity, entity.params.positional.condition, context, 'Condition') || {}; + const generatedBody = generateFromBody(entity.body, context); - const generatedElse = (entity.siblings && entity.siblings['else'] || []).map(elseEntity => { - const elseBody = generateFromBody(elseEntity.body, context); - return [ - '} else {', - indent(elseBody) - ].join('\n'); - }); + return [ + `if (${condition.code}) {`, + indent(generatedBody), + '}' + ].join('\n'); + }, + + 'elseif': (entity, context) => { + const condition = getExpression(entity, entity.params.positional.condition, context, 'Condition') || {}; + const generatedBody = generateFromBody(entity.body, context); + + return [ + `else if (${condition.code}) {`, + indent(generatedBody), + '}' + ].join('\n'); + }, + + 'else': (entity, context) => { + const generatedBody = generateFromBody(entity.body, context); return [ - `if (${ifCondition.code}) {`, - indent(ifBody), - generatedElseIf, - generatedElse, + 'else {', + indent(generatedBody), '}' ].join('\n'); } From 9326b53140b4894317ff152d9e44230f4c999ded Mon Sep 17 00:00:00 2001 From: Haroldo de Oliveira Pinheiro Date: Thu, 22 Sep 2022 20:33:22 -0300 Subject: [PATCH 5/6] Update: point that `if`/`elseif` and `endif` work. --- README.md | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c756d14..f981467 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,8 @@ The syntax of the scripts is somewhat based on ChoiceScript, but it is not exact *Please note that this is an early work and progress, and it is not as stable or user-friendly as it is planned to become.* + + ## Commands implemented so far ### `font` @@ -47,13 +49,16 @@ Creates a local variable. `temp` variables are only visible inside the scene fil Changes the current value of an existing variable. +### `if`/`elseif`/`else` + +Allows a certain block of code to only be executed on a given condition. + + + ## Planned commands The tool accepts those commands, but, at the moment, they don't do anything. -### `if`/`elseif`/`else` -Will allow a certain block of code to only be executed on a given condition. - ### `label` Will allow to mark a place where the `goto` command can jump to. From 58266a4ea231bcb7fd8cf2e257d49a3b42056d22 Mon Sep 17 00:00:00 2001 From: Haroldo de Oliveira Pinheiro Date: Thu, 22 Sep 2022 20:36:45 -0300 Subject: [PATCH 6/6] Bump version to 0.2.0 --- examples/test/src/vn_engine.c | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/test/src/vn_engine.c b/examples/test/src/vn_engine.c index ef6549b..18583ca 100644 --- a/examples/test/src/vn_engine.c +++ b/examples/test/src/vn_engine.c @@ -63,7 +63,7 @@ void VN_init() { XGM_setLoopNumber(-1); XGM_setForceDelayDMA(TRUE); - VDP_drawText("choice4genesis v0.1.0", 18, 27); + VDP_drawText("choice4genesis v0.2.0", 18, 27); } diff --git a/package.json b/package.json index cef359c..9d70cae 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "choice4genesis", - "version": "0.1.0", + "version": "0.2.0", "description": "A ChoiceScript clone that generates SGDK-compatible C source for the Sega Genesis ", "main": "index.js", "scripts": {