Skip to content

Commit 56a630f

Browse files
author
avi
committed
working basic conditions
1 parent 284b873 commit 56a630f

File tree

2 files changed

+70
-14
lines changed

2 files changed

+70
-14
lines changed

src/examples/expressions.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ console.log(a2)
2020
let strLitIdentifier = "abc"
2121
let a3 = a + ((2*3)+strLitIdentifier)
2222
console.log(a3)
23+
let a4 = a + (strLitIdentifier+(2*3))
24+
console.log(a4)
25+
2326

2427
let str = "abc"
2528
let str2= str + "def"
2629
console.log(str2)
30+
31+
str2 = "new value"
32+
console.log(str2)

src/syntax.ts

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,26 @@ export class Assemble {
134134
return new AssembledOutput(SyntaxKind.Identifier,"$"+node.getText())
135135
}
136136

137+
IdentifierFromIdentifierAccess(outputNode : AssembledOutput) :AssembledOutput {
138+
if(outputNode.kind != SyntaxKind.Identifier || outputNode.output[0]!="$"){
139+
throw new Error("Cannot convert a non-(IdentifierAccess) type to an identifier");
140+
}
141+
outputNode.output = outputNode.output.substr(1)
142+
return outputNode;
143+
}
144+
137145
Identifier(symbolTable : SymbolTable, node: Node) :string {
138146
return `${node.getText()}`
139147
}
140148

149+
TrueKeyword(symbolTable : SymbolTable, node: Node) :AssembledOutput {
150+
return new AssembledOutput(SyntaxKind.Identifier,"1")
151+
}
152+
153+
FalseKeyword(symbolTable : SymbolTable, node: Node) :AssembledOutput {
154+
return new AssembledOutput(SyntaxKind.Identifier,"0")
155+
}
156+
141157
StringLiteral(symbolTable : SymbolTable, node: Node) :AssembledOutput {
142158
return new AssembledOutput(SyntaxKind.StringLiteral,node.getText())
143159
}
@@ -168,7 +184,9 @@ export class Assemble {
168184
assembledOutput.push(this.BinaryExpression(symbolTable, node));
169185
} else if (node.kind == SyntaxKind.PrefixUnaryExpression) {
170186
assembledOutput.push(this.PrefixUnaryExpression(symbolTable, node));
171-
} else if (node.kind == SyntaxKind.Block) {
187+
} else if (node.kind == SyntaxKind.Identifier) {
188+
assembledOutput.push(this.IdentifierAccess(symbolTable, node));
189+
}else if (node.kind == SyntaxKind.Block) {
172190
assembledOutput.push(this.Block(symbolTable, node));
173191
} else if (node.kind == SyntaxKind.ElseKeyword) {
174192
assembledOutput.push(this.ElseKeyword(symbolTable, node));
@@ -232,6 +250,8 @@ export class Assemble {
232250
output+=this.CallExpression(symbolTable, node)
233251
}else if(node.kind == SyntaxKind.SemicolonToken){
234252
output+=("\n");
253+
}else if(node.kind == SyntaxKind.BinaryExpression){
254+
output+= this.BinaryExpression(symbolTable,node).flattenOutput()
235255
}else{
236256
this.unhandledToken(symbolTable, node)
237257
}
@@ -317,10 +337,10 @@ export class Assemble {
317337
output+=this.noop(symbolTable, node);
318338
symbolTable.insert(identifier, SymbolType.NUMBER)
319339
}else if (node.kind == SyntaxKind.TrueKeyword) {
320-
output+="1";
340+
output+=this.TrueKeyword(symbolTable,node).flattenOutput();
321341
symbolTable.insert(identifier, SymbolType.NUMBER)
322342
}else if (node.kind == SyntaxKind.FalseKeyword) {
323-
output+="0";
343+
output+=this.FalseKeyword(symbolTable,node).flattenOutput();
324344
symbolTable.insert(identifier, SymbolType.NUMBER)
325345
} else if (node.kind == SyntaxKind.StringKeyword) {
326346
output+=this.noop(symbolTable, node);
@@ -392,11 +412,11 @@ export class Assemble {
392412
}
393413

394414
ConditionOpenParenToken(symbolTable : SymbolTable, node: Node) :AssembledOutput {
395-
return new AssembledOutput(SyntaxKind.OpenParenToken," [[ ");
415+
return new AssembledOutput(SyntaxKind.OpenParenToken," (( ");
396416
}
397417

398418
ConditionCloseParenToken(symbolTable : SymbolTable, node: Node) :AssembledOutput {
399-
return new AssembledOutput(SyntaxKind.OpenParenToken," ]] ");
419+
return new AssembledOutput(SyntaxKind.OpenParenToken," )) ");
400420
}
401421

402422
OpenParenToken(symbolTable : SymbolTable, node: Node) :AssembledOutput {
@@ -430,6 +450,7 @@ export class Assemble {
430450
let operator = "";
431451
let operands :AssembledOutput[]=[]
432452
let hasStringLiteral = false;
453+
let isAssignment = false;
433454
let childNodes = node.getChildren();
434455
for (let i = 0; i < childNodes.length; i++) {
435456
let node = childNodes[i];
@@ -440,7 +461,11 @@ export class Assemble {
440461
operands.push(this.IdentifierAccess(symbolTable, node))
441462
} else if (node.kind == SyntaxKind.FirstLiteralToken) {
442463
operands.push(this.FirstLiteralToken(symbolTable,node))
443-
}else if (node.kind == SyntaxKind.StringLiteral) {
464+
} else if (node.kind == SyntaxKind.TrueKeyword) {
465+
operands.push(this.TrueKeyword(symbolTable,node))
466+
} else if (node.kind == SyntaxKind.FalseKeyword) {
467+
operands.push(this.FalseKeyword(symbolTable,node))
468+
} else if (node.kind == SyntaxKind.StringLiteral) {
444469
operands.push(this.StringLiteral(symbolTable,node))
445470
hasStringLiteral = true;
446471
} else if (node.kind == SyntaxKind.PlusToken
@@ -457,7 +482,10 @@ export class Assemble {
457482
if(operator=="==="){
458483
operator = "=="
459484
}
460-
} else if (node.kind == SyntaxKind.BinaryExpression) {
485+
} else if(node.kind == SyntaxKind.FirstAssignment){
486+
operator = node.getText();
487+
isAssignment = true;
488+
} else if (node.kind == SyntaxKind.BinaryExpression) {
461489
let binExpOutput = this.BinaryExpression(symbolTable, node)
462490
if(!binExpOutput.isNumeric){
463491
hasStringLiteral = true;
@@ -477,18 +505,40 @@ export class Assemble {
477505
let openParen = "$(("
478506
let closeParen = "))"
479507

480-
if(hasStringLiteral){
481-
operator=""
508+
if(hasStringLiteral || isAssignment){
509+
510+
//is this a concat operation?
511+
if(isAssignment == false && hasStringLiteral == true){
512+
operator=""
513+
}
482514
openParen = ""
483515
closeParen = ""
484516
assembledOutput.isNumeric = false;
485-
if(operands[0].isNumeric && operands[0].peek().kind == SyntaxKind.CloseParenToken){
486-
operands[0].pop(); //remove ")"
487-
let val = operands[0].pop(); //get value
488-
operands[0].pop(); // remove "("
489-
operands[0].push(val);
517+
518+
let removeParensIfNumeric = function(operand : AssembledOutput){
519+
if(operand.isNumeric && operand.peek().kind == SyntaxKind.CloseParenToken){
520+
operand.pop(); //remove ")"
521+
let val = operand.pop(); //get value
522+
operand.pop(); // remove "("
523+
operand.push(val);
524+
}
525+
return operand;
526+
}
527+
528+
operands[0] = removeParensIfNumeric(operands[0])
529+
operands[1] = removeParensIfNumeric(operands[1])
530+
531+
//a=4 and not $a=4
532+
if(isAssignment && operands[0].kind == SyntaxKind.Identifier){
533+
operands[0] = this.IdentifierFromIdentifierAccess(operands[0])
490534
}
491535

536+
// if(operands[0].isNumeric && operands[0].peek().kind == SyntaxKind.CloseParenToken){
537+
// operands[0].pop(); //remove ")"
538+
// let val = operands[0].pop(); //get value
539+
// operands[0].pop(); // remove "("
540+
// operands[0].push(val);
541+
// }
492542

493543
}else{
494544
assembledOutput.isNumeric = true;

0 commit comments

Comments
 (0)