Skip to content

Commit

Permalink
Implement if/else/elsif
Browse files Browse the repository at this point in the history
Merge pull request #31 from haroldo-ok/conditionals
  • Loading branch information
haroldo-ok authored Sep 22, 2022
2 parents b99fc02 + 58266a4 commit 80640d0
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 13 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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.

Expand Down
21 changes: 16 additions & 5 deletions examples/test/project/startup.choice
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,24 @@
* set intVar, 3
* set localInt, intVar + 3

* create playingMusic, false

* choice
# Play some music
* if playingMusic
# Stop the music
* stop music, sound
OK, music is stopped.
* set playingMusic, false
* elseif FALSE
This should not appear
This should not play
* music "Actraiser - Fillmore.vgm"
OK, playing Fillmore, from Actraiser.
# Stop the music
* stop music, sound
OK, music is stopped.
* 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.
Expand Down
2 changes: 1 addition & 1 deletion examples/test/src/vn_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down
34 changes: 34 additions & 0 deletions generator/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -228,6 +230,38 @@ const COMMAND_GENERATORS = {
}

return `${existingVar.value.internalVar} = ${newValue.code};`;
},

'if': (entity, context) => {
const condition = getExpression(entity, entity.params.positional.condition, context, 'Condition') || {};
const generatedBody = generateFromBody(entity.body, context);

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 [
'else {',
indent(generatedBody),
'}'
].join('\n');
}
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
4 changes: 1 addition & 3 deletions parser/syntax-full.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -139,6 +136,7 @@ const parse = source => {

const completedErrors = [...(errors || [])];
const completedBody = completeCommands(body, completedErrors);
debugger;

return {
type,
Expand Down

0 comments on commit 80640d0

Please sign in to comment.