Use uppercase field names in Token

This commit is contained in:
Brandon Dyck 2023-07-25 13:19:07 -06:00
parent 64fac5a9fb
commit a7421927ae
4 changed files with 23 additions and 23 deletions

6
AST.cs
View File

@ -11,7 +11,7 @@ public partial record Variable
{ {
public override string ToString() public override string ToString()
{ {
return this.Value.lexeme; return this.Value.Lexeme;
} }
} }
@ -19,7 +19,7 @@ public partial record Binary
{ {
public override string ToString() public override string ToString()
{ {
return $"Binary {{ Left = {this.Left}, Op = {this.Op.lexeme}, Right = {this.Right} }}"; return $"Binary {{ Left = {this.Left}, Op = {this.Op.Lexeme}, Right = {this.Right} }}";
} }
} }
@ -39,7 +39,7 @@ public partial record SimplePattern
{ {
return "_"; return "_";
} }
return this.Identifier.lexeme; return this.Identifier.Lexeme;
} }
} }

View File

@ -35,7 +35,7 @@ public class Env
{ {
set set
{ {
var name = (string)identifier.literal!; var name = (string)identifier.Literal!;
if (values.ContainsKey(name)) if (values.ContainsKey(name))
{ {
// TODO use real location info // TODO use real location info
@ -45,7 +45,7 @@ public class Env
} }
get get
{ {
var name = (string)identifier.literal!; var name = (string)identifier.Literal!;
try try
{ {
return values[name]; return values[name];
@ -98,7 +98,7 @@ public class Interpreter : AST.IExprVisitor<Env, object>
var removedLabels = new List<string>(); var removedLabels = new List<string>();
foreach (var field in pattern.Fields) foreach (var field in pattern.Fields)
{ {
string name = (string)field.Name.literal!; string name = (string)field.Name.Literal!;
removedLabels.Add(name); removedLabels.Add(name);
var fieldValue = r.Get(name); var fieldValue = r.Get(name);
field.accept((fieldValue, env), this); field.accept((fieldValue, env), this);
@ -130,7 +130,7 @@ public class Interpreter : AST.IExprVisitor<Env, object>
switch (obj) switch (obj)
{ {
case Variant v: case Variant v:
var tag = (string)pattern.Tag.literal!; var tag = (string)pattern.Tag.Literal!;
if (v.Tag != tag) if (v.Tag != tag)
{ {
throw new PatternTagMismatchException(tag, v.Tag); throw new PatternTagMismatchException(tag, v.Tag);
@ -341,7 +341,7 @@ public class Interpreter : AST.IExprVisitor<Env, object>
var left = evaluate(env, expr.Left); var left = evaluate(env, expr.Left);
var right = evaluate(env, expr.Right); var right = evaluate(env, expr.Right);
switch (expr.Op.type) switch (expr.Op.Type)
{ {
case TokenType.Minus: case TokenType.Minus:
checkNumberOperands(expr.Op, left, right); checkNumberOperands(expr.Op, left, right);
@ -504,7 +504,7 @@ public class Interpreter : AST.IExprVisitor<Env, object>
HashSet<string> updateLabels = new HashSet<string>(); HashSet<string> updateLabels = new HashSet<string>();
foreach (AST.Field update in expr.Base.Updates) foreach (AST.Field update in expr.Base.Updates)
{ {
var label = (string)update.Name.literal!; var label = (string)update.Name.Literal!;
if (updateLabels.Contains(label)) if (updateLabels.Contains(label))
{ {
throw new RuntimeError(tok, "Record updates must be to unique fields."); throw new RuntimeError(tok, "Record updates must be to unique fields.");
@ -527,7 +527,7 @@ public class Interpreter : AST.IExprVisitor<Env, object>
HashSet<string> extLabels = new HashSet<string>(); HashSet<string> extLabels = new HashSet<string>();
foreach (AST.Field extension in expr.Extensions) foreach (AST.Field extension in expr.Extensions)
{ {
var label = (string)extension.Name.literal!; var label = (string)extension.Name.Literal!;
if (extLabels.Contains(label)) if (extLabels.Contains(label))
{ {
throw new RuntimeError(tok, "Record extensions must have unique field names."); throw new RuntimeError(tok, "Record extensions must have unique field names.");
@ -549,7 +549,7 @@ public class Interpreter : AST.IExprVisitor<Env, object>
var r = checkRecordOperand(tok, left); var r = checkRecordOperand(tok, left);
try try
{ {
return r.Get((string)expr.FieldName.literal!); return r.Get((string)expr.FieldName.Literal!);
} }
catch catch
{ {
@ -566,7 +566,7 @@ public class Interpreter : AST.IExprVisitor<Env, object>
public object visitUnaryExpr(Env env, AST.Unary expr) public object visitUnaryExpr(Env env, AST.Unary expr)
{ {
object right = evaluate(env, expr.Right); object right = evaluate(env, expr.Right);
switch (expr.Op.type) switch (expr.Op.Type)
{ {
case TokenType.Minus: case TokenType.Minus:
checkNumberOperand(expr.Op, right); checkNumberOperand(expr.Op, right);
@ -588,7 +588,7 @@ public class Interpreter : AST.IExprVisitor<Env, object>
public object visitVariantExpr(Env env, AST.Variant expr) public object visitVariantExpr(Env env, AST.Variant expr)
{ {
var tag = (string)expr.Tag.literal!; var tag = (string)expr.Tag.Literal!;
if (expr.Argument == null) if (expr.Argument == null)
{ {
return new Variant(tag, null); return new Variant(tag, null);

View File

@ -47,7 +47,7 @@ class Parser
private bool check(params TokenType[] types) private bool check(params TokenType[] types)
{ {
if (isAtEnd()) return false; if (isAtEnd()) return false;
return Array.IndexOf(types, peek().type) >= 0; return Array.IndexOf(types, peek().Type) >= 0;
} }
private Token advance() private Token advance()
@ -58,7 +58,7 @@ class Parser
private bool isAtEnd() private bool isAtEnd()
{ {
return peek().type == TokenType.EOF; return peek().Type == TokenType.EOF;
} }
private Token peek() private Token peek()
@ -89,7 +89,7 @@ class Parser
while (!isAtEnd()) while (!isAtEnd())
{ {
switch (peek().type) switch (peek().Type)
{ {
case TokenType.Def: case TokenType.Def:
case TokenType.Type: case TokenType.Type:
@ -357,8 +357,8 @@ class Parser
{ {
if (match(TokenType.Number, TokenType.String)) if (match(TokenType.Number, TokenType.String))
{ {
object literal = previous().literal!; object literal = previous().Literal!;
return new Literal(previous().literal!); return new Literal(previous().Literal!);
} }
if (match(TokenType.Identifier, TokenType.QuotedIdentifier)) if (match(TokenType.Identifier, TokenType.QuotedIdentifier))

View File

@ -82,19 +82,19 @@ class Program
public static void error(Token token, String message) public static void error(Token token, String message)
{ {
if (token.type == TokenType.EOF) if (token.Type == TokenType.EOF)
{ {
report(token.position, " at end", message); report(token.Position, " at end", message);
} }
else else
{ {
report(token.position, " at '" + token.lexeme + "'", message); report(token.Position, " at '" + token.Lexeme + "'", message);
} }
} }
public static void runtimeError(RuntimeError err) public static void runtimeError(RuntimeError err)
{ {
Console.Error.WriteLine($"{err.Message}\n[{err.Token.position}]"); Console.Error.WriteLine($"{err.Message}\n[{err.Token.Position}]");
hadRuntimeError = true; hadRuntimeError = true;
} }
@ -167,7 +167,7 @@ public record struct Position(int Offset, int Line, int Column)
} }
} }
public record Token(TokenType type, String lexeme, Object? literal, Position position); public record Token(TokenType Type, String Lexeme, Object? Literal, Position Position);
class Scanner class Scanner
{ {