This file contains hidden or 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
//Token.cs | |
public class Token | |
{ | |
public TokenType Type { get; set; } | |
public string Literal { get; set; } | |
} |
This file contains hidden or 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
[Test] | |
public void TestLexerNextToken() | |
{ | |
string input = @"1 + 2 * (12 - 6) / 3 + 2 ^ 3 | |
+ ((4+3) * 4) | |
/ 4 | |
"; | |
var tests = new[] | |
{ |
This file contains hidden or 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
// Lexer.cs | |
public class Lexer | |
{ | |
//... | |
public Token NextToken() | |
{ | |
//... | |
char character = this.input[this.position]; | |
switch (character) | |
{ |
This file contains hidden or 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
public abstract class Expression | |
{ | |
public abstract string TokenLiteral(); | |
} | |
//IntegerExpression.cs | |
public class IntegerExpression : Expression | |
{ | |
public Token Token { get; set; } | |
public long Value { get; set; } | |
//... |
This file contains hidden or 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
//Pecedence.cs | |
public enum Precedence | |
{ | |
LOWEST = 0, | |
SUM = 1, | |
PRODUCT = 2, | |
PREFIX = 3, | |
POWER = 4, | |
GROUP = 5, | |
} |
This file contains hidden or 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
public class Parser | |
{ | |
//... | |
public Expression Parse() | |
{ | |
return this.ParseExpression(Precedence.LOWEST); | |
} | |
private Expression ParseExpression(Precedence precedence) | |
{ | |
if(this.PrefixFns.ContainsKey(this._currentToken.Type)) |
This file contains hidden or 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
// ParserTest.cs | |
[TestFixture] | |
public class ParserTest | |
{ | |
//... | |
[Test] | |
public void TestParser() | |
{ | |
var tests = new [] | |
{ |
This file contains hidden or 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
// Object.cs | |
public abstract class Object | |
{ | |
public abstract ObjectType Type(); | |
public abstract string Inspect(); | |
} | |
// IntegerObject.cs | |
public class IntegerObject : Object | |
{ | |
public long Value { get; set; } |
This file contains hidden or 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
// Evalutor.cs | |
public class Evaluator | |
{ | |
public static Object.Object Eval(Expression expression) | |
{ | |
if(expression is AST.IntegerExpression) | |
{ | |
return new Object.IntegerObject { Value = ((IntegerExpression)expression).Value }; | |
} | |
if(expression is AST.PrefixExpression) |
This file contains hidden or 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
static void Main(string[] args) | |
{ | |
var lexer = new Lexer.Lexer(input); | |
var parser = new Parser.Parser(lexer); | |
var expression = parser.Parse(); | |
var result = Evaluate.Evaluator.Eval(expression); | |
Console.Out.Write(result.Inspect()); | |
} |