This commit is contained in:
Deukhoofd 2018-11-14 13:45:49 +01:00
parent d34e5c85c7
commit 3efa6a6359
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
6 changed files with 27 additions and 6 deletions

View File

@ -0,0 +1,12 @@
namespace Upsilon.BaseTypes
{
public class LuaNull : LuaType
{
public override Type Type => Type.Nil;
public override string ToString()
{
return "null";
}
}
}

View File

@ -110,11 +110,15 @@ namespace Upsilon.Binder
type = Type.Boolean; type = Type.Boolean;
outValue = new LuaBoolean(b); outValue = new LuaBoolean(b);
break; break;
case null:
type = Type.Nil;
outValue = new LuaNull();
break;
default: default:
_diagnostics.LogUnknownType(e.Span); _diagnostics.LogUnknownType(e.Span);
break; break;
} }
return new BoundLiteralExpression(outValue, type); return new BoundLiteralExpression(outValue);
} }
private BoundExpression BindParenthesizedExpression(ParenthesizedExpressionSyntax e) private BoundExpression BindParenthesizedExpression(ParenthesizedExpressionSyntax e)
@ -128,7 +132,7 @@ namespace Upsilon.Binder
if (!_scope.TryGetVariable(name, out var variable)) if (!_scope.TryGetVariable(name, out var variable))
{ {
_diagnostics.LogUnknownVariable(e.Identifier.Span, name); _diagnostics.LogUnknownVariable(e.Identifier.Span, name);
return new BoundLiteralExpression(null, Type.Nil); return new BoundLiteralExpression(new LuaNull());
} }
return new BoundVariableExpression(variable); return new BoundVariableExpression(variable);

View File

@ -4,14 +4,13 @@ namespace Upsilon.Binder
{ {
public class BoundLiteralExpression : BoundExpression public class BoundLiteralExpression : BoundExpression
{ {
public BoundLiteralExpression(LuaType value, Type type) public BoundLiteralExpression(LuaType value)
{ {
Value = value; Value = value;
Type = type;
} }
public override BoundKind Kind => BoundKind.BoundLiteralExpression; public override BoundKind Kind => BoundKind.BoundLiteralExpression;
public override Type Type { get; } public override Type Type => Value.Type;
public LuaType Value { get; } public LuaType Value { get; }
} }
} }

View File

@ -181,6 +181,9 @@ namespace Upsilon.Parser
case SyntaxKind.Identifier: case SyntaxKind.Identifier:
var token = MatchToken(SyntaxKind.Identifier); var token = MatchToken(SyntaxKind.Identifier);
return new VariableExpressionSyntax((IdentifierToken) token); return new VariableExpressionSyntax((IdentifierToken) token);
case SyntaxKind.NilKeyword:
var nilToken = MatchToken(SyntaxKind.NilKeyword);
return new LiteralExpressionSyntax(nilToken, null);
default: default:
_diagnostics.LogBadCharacter(new TextSpan(_position, 1), SyntaxKind.Identifier); _diagnostics.LogBadCharacter(new TextSpan(_position, 1), SyntaxKind.Identifier);
NextToken(); NextToken();

View File

@ -28,6 +28,8 @@ namespace Upsilon.Parser
return SyntaxKind.ElseIfKeyword; return SyntaxKind.ElseIfKeyword;
case "else": case "else":
return SyntaxKind.ElseKeyword; return SyntaxKind.ElseKeyword;
case "nil":
return SyntaxKind.NilKeyword;
default: default:
return SyntaxKind.Identifier; return SyntaxKind.Identifier;
} }

View File

@ -32,6 +32,7 @@ namespace Upsilon.Parser
ThenKeyword, ThenKeyword,
ElseIfKeyword, ElseIfKeyword,
ElseKeyword, ElseKeyword,
NilKeyword,
Identifier, Identifier,
@ -52,6 +53,6 @@ namespace Upsilon.Parser
BlockStatement, BlockStatement,
IfStatement, IfStatement,
ElseIfStatement, ElseIfStatement,
ElseStatement ElseStatement,
} }
} }