diff --git a/Nub.Lang/Nub.Lang/Lexing/Lexer.cs b/Nub.Lang/Nub.Lang/Lexing/Lexer.cs index cf969db..0e9b444 100644 --- a/Nub.Lang/Nub.Lang/Lexing/Lexer.cs +++ b/Nub.Lang/Nub.Lang/Lexing/Lexer.cs @@ -11,6 +11,14 @@ public class Lexer ["let"] = Symbol.Let, }; + private static readonly Dictionary Chians = new() + { + [['=', '=']] = Symbol.Equal, + [['!', '=']] = Symbol.NotEqual, + [['<', '=']] = Symbol.LessThanOrEqual, + [['>', '=']] = Symbol.GreaterThanOrEqual, + }; + private static readonly Dictionary Chars = new() { [';'] = Symbol.Semicolon, @@ -26,6 +34,11 @@ public class Lexer ['='] = Symbol.Assign, ['<'] = Symbol.LessThan, ['>'] = Symbol.GreaterThan, + ['+'] = Symbol.Plus, + ['-'] = Symbol.Minus, + ['*'] = Symbol.Star, + ['/'] = Symbol.ForwardSlash, + ['!'] = Symbol.Bang, }; private readonly string _src; @@ -84,6 +97,22 @@ public class Lexer return new LiteralToken(new PrimitiveType(PrimitiveTypeKind.Int64), buffer); } + foreach (var chain in Chians) + { + if (current.Value != chain.Key[0]) continue; + + for (var i = 1; i < chain.Key.Length; i++) + { + var c = Peek(i); + if (!c.HasValue || c.Value != chain.Key[i]) break; + + if (i == chain.Key.Length - 1) + { + return new SymbolToken(chain.Value); + } + } + } + if (Chars.TryGetValue(current.Value, out var charSymbol)) { Next(); @@ -116,11 +145,11 @@ public class Lexer throw new Exception($"Unknown character {current.Value}"); } - private Optional Peek() + private Optional Peek(int offset = 0) { - if (_index < _src.Length) + if (_index + offset < _src.Length) { - return _src[_index]; + return _src[_index + offset]; } return Optional.Empty(); diff --git a/Nub.Lang/Nub.Lang/Lexing/SymbolToken.cs b/Nub.Lang/Nub.Lang/Lexing/SymbolToken.cs index 4ce058b..29f8ce6 100644 --- a/Nub.Lang/Nub.Lang/Lexing/SymbolToken.cs +++ b/Nub.Lang/Nub.Lang/Lexing/SymbolToken.cs @@ -19,9 +19,18 @@ public enum Symbol CloseBrace, OpenBracket, CloseBracket, - LessThan, - GreaterThan, Comma, Period, - Assign + Assign, + Equal, + Bang, + NotEqual, + LessThan, + LessThanOrEqual, + GreaterThan, + GreaterThanOrEqual, + Plus, + Minus, + Star, + ForwardSlash, } \ No newline at end of file diff --git a/Nub.Lang/Nub.Lang/Parsing/BinaryExpressionNode.cs b/Nub.Lang/Nub.Lang/Parsing/BinaryExpressionNode.cs new file mode 100644 index 0000000..9d440d0 --- /dev/null +++ b/Nub.Lang/Nub.Lang/Parsing/BinaryExpressionNode.cs @@ -0,0 +1,22 @@ +namespace Nub.Lang.Parsing; + +public class BinaryExpressionNode(ExpressionNode left, BinaryExpressionOperator @operator, ExpressionNode right) : ExpressionNode +{ + public ExpressionNode Left { get; } = left; + public BinaryExpressionOperator Operator { get; } = @operator; + public ExpressionNode Right { get; } = right; +} + +public enum BinaryExpressionOperator +{ + Equal, + NotEqual, + GreaterThan, + GreaterThanOrEqual, + LessThan, + LessThanOrEqual, + Plus, + Minus, + Multiply, + Divide +} \ No newline at end of file