From 3efa6a63598b47859536c214678370b51e37d10f Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Wed, 14 Nov 2018 13:45:49 +0100 Subject: [PATCH] Adds Nil --- Upsilon/BaseTypes/LuaNull.cs | 12 ++++++++++++ Upsilon/Binder/Binder.cs | 8 ++++++-- .../BoundExpressions/BoundLiteralExpression.cs | 5 ++--- Upsilon/Parser/Parser.cs | 3 +++ Upsilon/Parser/SyntaxKeyWords.cs | 2 ++ Upsilon/Parser/SyntaxKind.cs | 3 ++- 6 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 Upsilon/BaseTypes/LuaNull.cs diff --git a/Upsilon/BaseTypes/LuaNull.cs b/Upsilon/BaseTypes/LuaNull.cs new file mode 100644 index 0000000..82637c5 --- /dev/null +++ b/Upsilon/BaseTypes/LuaNull.cs @@ -0,0 +1,12 @@ +namespace Upsilon.BaseTypes +{ + public class LuaNull : LuaType + { + public override Type Type => Type.Nil; + + public override string ToString() + { + return "null"; + } + } +} \ No newline at end of file diff --git a/Upsilon/Binder/Binder.cs b/Upsilon/Binder/Binder.cs index 27dcc97..a216bbd 100644 --- a/Upsilon/Binder/Binder.cs +++ b/Upsilon/Binder/Binder.cs @@ -110,11 +110,15 @@ namespace Upsilon.Binder type = Type.Boolean; outValue = new LuaBoolean(b); break; + case null: + type = Type.Nil; + outValue = new LuaNull(); + break; default: _diagnostics.LogUnknownType(e.Span); break; } - return new BoundLiteralExpression(outValue, type); + return new BoundLiteralExpression(outValue); } private BoundExpression BindParenthesizedExpression(ParenthesizedExpressionSyntax e) @@ -128,7 +132,7 @@ namespace Upsilon.Binder if (!_scope.TryGetVariable(name, out var variable)) { _diagnostics.LogUnknownVariable(e.Identifier.Span, name); - return new BoundLiteralExpression(null, Type.Nil); + return new BoundLiteralExpression(new LuaNull()); } return new BoundVariableExpression(variable); diff --git a/Upsilon/Binder/BoundExpressions/BoundLiteralExpression.cs b/Upsilon/Binder/BoundExpressions/BoundLiteralExpression.cs index 0d46d16..1533ad7 100644 --- a/Upsilon/Binder/BoundExpressions/BoundLiteralExpression.cs +++ b/Upsilon/Binder/BoundExpressions/BoundLiteralExpression.cs @@ -4,14 +4,13 @@ namespace Upsilon.Binder { public class BoundLiteralExpression : BoundExpression { - public BoundLiteralExpression(LuaType value, Type type) + public BoundLiteralExpression(LuaType value) { Value = value; - Type = type; } public override BoundKind Kind => BoundKind.BoundLiteralExpression; - public override Type Type { get; } + public override Type Type => Value.Type; public LuaType Value { get; } } } \ No newline at end of file diff --git a/Upsilon/Parser/Parser.cs b/Upsilon/Parser/Parser.cs index 3c3bf64..9deb1be 100644 --- a/Upsilon/Parser/Parser.cs +++ b/Upsilon/Parser/Parser.cs @@ -181,6 +181,9 @@ namespace Upsilon.Parser case SyntaxKind.Identifier: var token = MatchToken(SyntaxKind.Identifier); return new VariableExpressionSyntax((IdentifierToken) token); + case SyntaxKind.NilKeyword: + var nilToken = MatchToken(SyntaxKind.NilKeyword); + return new LiteralExpressionSyntax(nilToken, null); default: _diagnostics.LogBadCharacter(new TextSpan(_position, 1), SyntaxKind.Identifier); NextToken(); diff --git a/Upsilon/Parser/SyntaxKeyWords.cs b/Upsilon/Parser/SyntaxKeyWords.cs index 850d0ae..3c58532 100644 --- a/Upsilon/Parser/SyntaxKeyWords.cs +++ b/Upsilon/Parser/SyntaxKeyWords.cs @@ -28,6 +28,8 @@ namespace Upsilon.Parser return SyntaxKind.ElseIfKeyword; case "else": return SyntaxKind.ElseKeyword; + case "nil": + return SyntaxKind.NilKeyword; default: return SyntaxKind.Identifier; } diff --git a/Upsilon/Parser/SyntaxKind.cs b/Upsilon/Parser/SyntaxKind.cs index f84d31c..576c6b6 100644 --- a/Upsilon/Parser/SyntaxKind.cs +++ b/Upsilon/Parser/SyntaxKind.cs @@ -32,6 +32,7 @@ namespace Upsilon.Parser ThenKeyword, ElseIfKeyword, ElseKeyword, + NilKeyword, Identifier, @@ -52,6 +53,6 @@ namespace Upsilon.Parser BlockStatement, IfStatement, ElseIfStatement, - ElseStatement + ElseStatement, } } \ No newline at end of file