Skip to content

Commit

Permalink
update expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
timruffles committed Apr 19, 2019
1 parent c883286 commit 8a1afcf
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 6 deletions.
41 changes: 36 additions & 5 deletions src/js-to-c.ts
Expand Up @@ -28,7 +28,7 @@ import {
ThrowStatement,
TryStatement,
UnaryExpression,
UnaryOperator,
UnaryOperator, UpdateExpression,
VariableDeclaration,
VariableDeclarator,
WhileStatement,
Expand Down Expand Up @@ -173,6 +173,27 @@ function compileLogicalExpression(node: LogicalExpression, state: CompileTimeSta
}


function compileUpdateExpression(node: UpdateExpression, state: CompileTimeState) {
const value = node.operator === '++' ? 1 : -1
const evaluatedTo = new IntermediateVariableTarget(state.getNextSymbol('eval'))
const updateSrc = compileAssignmentExpression({
type: 'AssignmentExpression',
operator: '+=',
left: node.argument as any,
right: {
type: 'Literal',
value,
}
}, state.childStateWithTarget(evaluatedTo))
const finalSrc = node.prefix
? evaluatedTo.id
// in post-fix case, calculate the value we had before
: `addOperator(${evaluatedTo.id}, jsValueCreateNumber(${-value}))`
return `${updateSrc}
${assignToTarget(finalSrc, state.target)}
`
}

function getCompilers(): NodeCompilerLookup {
return {
ArrayExpression: compileArrayExpression,
Expand Down Expand Up @@ -201,22 +222,22 @@ function getCompilers(): NodeCompilerLookup {
NewExpression: compileNewExpression,
ObjectExpression: compileObjectExpression,
Program: compileProgram,
Property: unimplemented('Property'),
ReturnStatement: compileReturnStatement,
SequenceExpression: unimplemented('SequenceExpression'),
SwitchStatement: compileSwitchStatement,
ThisExpression: compileThisExpression,
ThrowStatement: compileThrowStatement,
TryStatement: compileTryStatement,
UnaryExpression: compileUnaryExpression,
UpdateExpression: unimplemented('UpdateExpression'),
UpdateExpression: compileUpdateExpression,
VariableDeclaration: compileVariableDeclaration,
VariableDeclarator: compileVariableDeclarator,
WhileStatement: compileWhileStatement,

// Sub-expressions
CatchClause: unimplemented('CatchClause'), // NOTE: handled in TryStatement
SwitchCase: unimplemented('SwitchCase'), // NOTE: handled in SwitchStatement
Property: unimplemented('Property'),

// Leaving out - strict mode
WithStatement: notInStrictMode('WithStatement'),
Expand Down Expand Up @@ -873,6 +894,14 @@ function compileArrayExpression(node: ArrayExpression, state: CompileTimeState)
function compileAssignmentExpression(node: AssignmentExpression, state: CompileTimeState) {
const target = node.left;
const result = new PredefinedVariableTarget(state.getNextSymbol('result'));

// TODO - add pre/post fix logic
// - standard = prefix, so we end up with the assigned value
// - postfix, e.g ++, end up with initial value
// so postfix we need to either:
// - capture initial value
// - or, recaluate it at cost of extra op

switch(target.type) {
case 'Identifier': {
if(target.name === 'this') {
Expand All @@ -887,7 +916,8 @@ function compileAssignmentExpression(node: AssignmentExpression, state: CompileT
return `JsValue* ${result.id};
${compile(node.right, state.childState({ target: result }))}
${update}
envSet(env, ${variable.id}, ${result.id});`
envSet(env, ${variable.id}, ${result.id});
${assignToTarget(result.id, state.target)}`
}
case 'MemberExpression': {
// order of execution - target, prop, value
Expand All @@ -908,7 +938,8 @@ function compileAssignmentExpression(node: AssignmentExpression, state: CompileT
${propertySrc} // property src
${compile(node.right, state.childState({ target: result }))} // RHS
${update} // any update
objectSet(${assignmentTarget.id}, ${propertyTarget.id}, ${result.id}); // obj set`
objectSet(${assignmentTarget.id}, ${propertyTarget.id}, ${result.id}); // obj set
${assignToTarget(result.id, state.target)}`
}
default:
return unimplemented(target.type)();
Expand Down
2 changes: 1 addition & 1 deletion test/forLoop.toml
Expand Up @@ -3,7 +3,7 @@
example = """
for(var i = 0;
i < 5;
i += 1) {
i++) {
}
console.log(i)
"""
Expand Down
52 changes: 52 additions & 0 deletions test/operators.toml
Expand Up @@ -112,3 +112,55 @@ console.log(10 > 15 ? "fail" : "ok");
"""

output="ok\nok\nok"

["prefix add"]

example="""
var a = 42
console.log(++a);
console.log(a);
"""

output="43\n43"

["postfix add"]

example="""
var a = 42
console.log(a++);
console.log(a);
"""

output="42\n43"

["prefix subtract"]

example="""
var a = 42
console.log(--a);
console.log(a);
"""


output="41\n41"

["postfix subtract"]

example="""
var a = 42
console.log(a--);
console.log(a);
"""

output="42\n41"

["update as expression"]

example="""
var a = 42
var b = a += 45
console.log(a)
console.log(b)
"""

output="87\n87"

0 comments on commit 8a1afcf

Please sign in to comment.