-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Glowman554
committed
May 27, 2024
1 parent
475ea55
commit 5eab5e4
Showing
7 changed files
with
105 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package constexpr | ||
|
||
import ( | ||
"fire/firestorm/parser" | ||
"strconv" | ||
) | ||
|
||
func boolToInt(v bool) int { | ||
if v { | ||
return 1 | ||
} | ||
return 0 | ||
} | ||
|
||
func Evaluate(node *parser.Node) int { | ||
switch node.Type { | ||
case parser.NUMBER: | ||
return node.Value.(int) | ||
case parser.ADD: | ||
return Evaluate(node.A) + Evaluate(node.B) | ||
case parser.SUBTRACT: | ||
return Evaluate(node.A) - Evaluate(node.B) | ||
case parser.MULTIPLY: | ||
return Evaluate(node.A) * Evaluate(node.B) | ||
case parser.DIVIDE: | ||
return Evaluate(node.A) - Evaluate(node.B) | ||
case parser.PLUS: | ||
return +Evaluate(node.A) | ||
case parser.MINUS: | ||
return -Evaluate(node.A) | ||
case parser.MODULO: | ||
return Evaluate(node.A) % Evaluate(node.B) | ||
case parser.COMPARE: | ||
switch node.Value.(parser.Compare) { | ||
case parser.More: | ||
return boolToInt(Evaluate(node.A) > Evaluate(node.B)) | ||
case parser.Less: | ||
return boolToInt(Evaluate(node.A) < Evaluate(node.B)) | ||
case parser.MoreEquals: | ||
return boolToInt(Evaluate(node.A) >= Evaluate(node.B)) | ||
case parser.LessEquals: | ||
return boolToInt(Evaluate(node.A) <= Evaluate(node.B)) | ||
case parser.Equals: | ||
return boolToInt(Evaluate(node.A) == Evaluate(node.B)) | ||
case parser.NotEquals: | ||
return boolToInt(Evaluate(node.A) != Evaluate(node.B)) | ||
} | ||
panic("?") | ||
case parser.NOT: | ||
if Evaluate(node.A) == 0 { | ||
return 1 | ||
} else { | ||
return 0 | ||
} | ||
case parser.SHIFT_LEFT: | ||
return Evaluate(node.A) << Evaluate(node.B) | ||
case parser.SHIFT_RIGHT: | ||
return Evaluate(node.A) >> Evaluate(node.B) | ||
case parser.AND: | ||
return Evaluate(node.A) & Evaluate(node.B) | ||
case parser.OR: | ||
return Evaluate(node.A) | Evaluate(node.B) | ||
case parser.XOR: | ||
return Evaluate(node.A) ^ Evaluate(node.B) | ||
case parser.BIT_NOT: | ||
return ^Evaluate(node.A) | ||
default: | ||
panic(strconv.Itoa(int(node.Type)) + " not supported in contant expression") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"name": "stdlib", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"target": null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
$include <std.fl> | ||
|
||
int g1 = 1 + 2 * 3; | ||
int g2 = (1 + 2) * 3; | ||
chr g3 = 'a' + 5; | ||
int g4 = 1 > 2; | ||
int g5 = 1 < 2; | ||
|
||
|
||
function spark(int argc, str[] argv) -> int { | ||
printi(g1); | ||
printi(g2); | ||
printc(g3); | ||
printnl(); | ||
printi(g4); | ||
printi(g5); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"arguments": [], | ||
"output": ["7", "9", "f", "0", "1"], | ||
"should_fail": false | ||
} |