From a7421927ae77c25eeff01e1dcb61b33d3d994e12 Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Tue, 25 Jul 2023 13:19:07 -0600 Subject: [PATCH] Use uppercase field names in Token --- AST.cs | 6 +++--- Interpreter.cs | 20 ++++++++++---------- Parser.cs | 10 +++++----- Program.cs | 10 +++++----- 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/AST.cs b/AST.cs index 187acd9..18ba0a6 100644 --- a/AST.cs +++ b/AST.cs @@ -11,7 +11,7 @@ public partial record Variable { public override string ToString() { - return this.Value.lexeme; + return this.Value.Lexeme; } } @@ -19,7 +19,7 @@ public partial record Binary { 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 this.Identifier.lexeme; + return this.Identifier.Lexeme; } } diff --git a/Interpreter.cs b/Interpreter.cs index c08bda3..03d3b55 100644 --- a/Interpreter.cs +++ b/Interpreter.cs @@ -35,7 +35,7 @@ public class Env { set { - var name = (string)identifier.literal!; + var name = (string)identifier.Literal!; if (values.ContainsKey(name)) { // TODO use real location info @@ -45,7 +45,7 @@ public class Env } get { - var name = (string)identifier.literal!; + var name = (string)identifier.Literal!; try { return values[name]; @@ -98,7 +98,7 @@ public class Interpreter : AST.IExprVisitor var removedLabels = new List(); foreach (var field in pattern.Fields) { - string name = (string)field.Name.literal!; + string name = (string)field.Name.Literal!; removedLabels.Add(name); var fieldValue = r.Get(name); field.accept((fieldValue, env), this); @@ -130,7 +130,7 @@ public class Interpreter : AST.IExprVisitor switch (obj) { case Variant v: - var tag = (string)pattern.Tag.literal!; + var tag = (string)pattern.Tag.Literal!; if (v.Tag != tag) { throw new PatternTagMismatchException(tag, v.Tag); @@ -341,7 +341,7 @@ public class Interpreter : AST.IExprVisitor var left = evaluate(env, expr.Left); var right = evaluate(env, expr.Right); - switch (expr.Op.type) + switch (expr.Op.Type) { case TokenType.Minus: checkNumberOperands(expr.Op, left, right); @@ -504,7 +504,7 @@ public class Interpreter : AST.IExprVisitor HashSet updateLabels = new HashSet(); foreach (AST.Field update in expr.Base.Updates) { - var label = (string)update.Name.literal!; + var label = (string)update.Name.Literal!; if (updateLabels.Contains(label)) { throw new RuntimeError(tok, "Record updates must be to unique fields."); @@ -527,7 +527,7 @@ public class Interpreter : AST.IExprVisitor HashSet extLabels = new HashSet(); foreach (AST.Field extension in expr.Extensions) { - var label = (string)extension.Name.literal!; + var label = (string)extension.Name.Literal!; if (extLabels.Contains(label)) { throw new RuntimeError(tok, "Record extensions must have unique field names."); @@ -549,7 +549,7 @@ public class Interpreter : AST.IExprVisitor var r = checkRecordOperand(tok, left); try { - return r.Get((string)expr.FieldName.literal!); + return r.Get((string)expr.FieldName.Literal!); } catch { @@ -566,7 +566,7 @@ public class Interpreter : AST.IExprVisitor public object visitUnaryExpr(Env env, AST.Unary expr) { object right = evaluate(env, expr.Right); - switch (expr.Op.type) + switch (expr.Op.Type) { case TokenType.Minus: checkNumberOperand(expr.Op, right); @@ -588,7 +588,7 @@ public class Interpreter : AST.IExprVisitor public object visitVariantExpr(Env env, AST.Variant expr) { - var tag = (string)expr.Tag.literal!; + var tag = (string)expr.Tag.Literal!; if (expr.Argument == null) { return new Variant(tag, null); diff --git a/Parser.cs b/Parser.cs index 5a8f219..0792dd0 100644 --- a/Parser.cs +++ b/Parser.cs @@ -47,7 +47,7 @@ class Parser private bool check(params TokenType[] types) { if (isAtEnd()) return false; - return Array.IndexOf(types, peek().type) >= 0; + return Array.IndexOf(types, peek().Type) >= 0; } private Token advance() @@ -58,7 +58,7 @@ class Parser private bool isAtEnd() { - return peek().type == TokenType.EOF; + return peek().Type == TokenType.EOF; } private Token peek() @@ -89,7 +89,7 @@ class Parser while (!isAtEnd()) { - switch (peek().type) + switch (peek().Type) { case TokenType.Def: case TokenType.Type: @@ -357,8 +357,8 @@ class Parser { if (match(TokenType.Number, TokenType.String)) { - object literal = previous().literal!; - return new Literal(previous().literal!); + object literal = previous().Literal!; + return new Literal(previous().Literal!); } if (match(TokenType.Identifier, TokenType.QuotedIdentifier)) diff --git a/Program.cs b/Program.cs index 90acdb4..77b4548 100644 --- a/Program.cs +++ b/Program.cs @@ -82,19 +82,19 @@ class Program 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 { - report(token.position, " at '" + token.lexeme + "'", message); + report(token.Position, " at '" + token.Lexeme + "'", message); } } 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; } @@ -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 {