-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Add builtin operators and fix parsing
- Loading branch information
1 parent
18e52bb
commit 7a06ad4
Showing
7 changed files
with
175 additions
and
12 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
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,42 @@ | ||
using Marionette.Parser; | ||
|
||
namespace Marionette.Runtime { | ||
public class BinaryOperator : IEvaluatable<Value, Environment> { | ||
private string name; | ||
|
||
private Value Add(Value lhs, Value rhs) { | ||
if (lhs is LiteralValue<int> i1 && rhs is LiteralValue<int> i2) { | ||
return new LiteralValue<int>(i1.Value + i2.Value); | ||
} | ||
else if (lhs is LiteralValue<int> i && rhs is LiteralValue<double> d) { | ||
return new LiteralValue<double>(i.Value + d.Value); | ||
} | ||
else if (lhs is LiteralValue<double> d2 && rhs is LiteralValue<int> i3) { | ||
return new LiteralValue<double>(i3.Value + d2.Value); | ||
} | ||
else if (lhs is LiteralValue<double> d3 && rhs is LiteralValue<double> d4) { | ||
return new LiteralValue<double>(d3.Value + d4.Value); | ||
} | ||
else if (lhs is LiteralValue<string> s1 && rhs is LiteralValue<string> s2) { | ||
return new LiteralValue<string>(s1.Value + s2.Value); | ||
} | ||
|
||
throw new Exception(string.Format("cannot add {0} and {1}.", lhs.Show(), rhs.Show())); | ||
} | ||
|
||
public Value Evaluate(Environment env) { | ||
var lhs = new EvalSymbol("lhs").Evaluate(env); | ||
var rhs = new EvalSymbol("rhs").Evaluate(env); | ||
if (name == "+") { | ||
return Add(lhs, rhs); | ||
} | ||
// TODO: other | ||
|
||
throw new Exception(string.Format("{0} cannot be used as a builtin symbol here.", name)); | ||
} | ||
|
||
public BinaryOperator(string name) { | ||
this.name = name; | ||
} | ||
} | ||
} // namespace Marionette.Runtime |
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
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,28 @@ | ||
(+ 1 1) | ||
;;|| --- Result --- || | ||
;;|| Res: 2 | ||
|
||
|
||
(+ 3.14 6.28) | ||
;;|| --- Result --- || | ||
;;|| Res: 9.42 | ||
|
||
|
||
(+ 0b1100 0b0011) | ||
;;|| --- Result --- || | ||
;;|| Res: 15 | ||
|
||
|
||
(+ 1 1.14) | ||
;;|| --- Result --- || | ||
;;|| Res: 2.1399999999999997 | ||
|
||
|
||
(+ 5.14 0) | ||
;;|| --- Result --- || | ||
;;|| Res: 5.14 | ||
|
||
|
||
(+ "hello " "world") | ||
;;|| --- Result --- || | ||
;;|| Res: hello world |