-
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.
Parser with first tests; ICommand as abstraction
- Loading branch information
Showing
6 changed files
with
83 additions
and
52 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
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,8 @@ | ||
| ||
namespace AjLang.Commands | ||
{ | ||
public interface ICommand | ||
{ | ||
void Execute(Context context); | ||
} | ||
} |
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,45 @@ | ||
namespace AjLang.Compiler | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using AjLang.Expressions; | ||
using System.Globalization; | ||
|
||
public class Parser | ||
{ | ||
private Lexer lexer; | ||
|
||
public Parser(string text) | ||
: this(new Lexer(text)) | ||
{ | ||
} | ||
|
||
public Parser(Lexer lexer) | ||
{ | ||
this.lexer = lexer; | ||
} | ||
|
||
public IExpression ParseExpression() | ||
{ | ||
Token token = this.NextToken(); | ||
|
||
if (token == null) | ||
return null; | ||
|
||
switch (token.Type) | ||
{ | ||
case TokenType.Integer: | ||
return new ConstantExpression(int.Parse(token.Value, CultureInfo.InvariantCulture)); | ||
} | ||
|
||
throw new ParserException(string.Format("Unexpected '{0}'", token.Value)); | ||
} | ||
|
||
private Token NextToken() | ||
{ | ||
return this.lexer.NextToken(); | ||
} | ||
} | ||
} |
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,15 @@ | ||
namespace AjLang.Compiler | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
public class ParserException : Exception | ||
{ | ||
public ParserException(string msg) | ||
: base(msg) | ||
{ | ||
} | ||
} | ||
} |