diff --git a/ssc/ast/nodes.cs b/ssc/ast/nodes.cs index 42d04a8..69dac4b 100644 --- a/ssc/ast/nodes.cs +++ b/ssc/ast/nodes.cs @@ -132,25 +132,12 @@ namespace arookas abstract class sunToken : sunNode { - public TValue Value { get; private set; } + public TValue Value { get; protected set; } - protected sunToken(sunSourceLocation location, string token) + protected sunToken(sunSourceLocation location) : base(location) { - Value = ParseValue(token); - } - - protected abstract TValue ParseValue(string token); - } - - abstract class sunRawToken : sunToken - { - protected sunRawToken(sunSourceLocation location, string token) - : base(location, token) - { } - - protected override string ParseValue(string token) { return token; } } } diff --git a/ssc/ast/nodes.literals.cs b/ssc/ast/nodes.literals.cs index 9406e5d..d23592c 100644 --- a/ssc/ast/nodes.literals.cs +++ b/ssc/ast/nodes.literals.cs @@ -6,13 +6,16 @@ namespace arookas { class sunIntLiteral : sunToken // base-10 integer { - public sunIntLiteral(sunSourceLocation location, string token) - : base(location, token) + public sunIntLiteral(sunSourceLocation location, string literal) + : base(location) { - + Value = Int32.Parse(literal); + } + protected sunIntLiteral(sunSourceLocation location) + : base(location) + { + // this overload lets protected classes set the value to something else } - - protected override int ParseValue(string token) { return Int32.Parse(token); } public override void Compile(sunContext context) { @@ -22,38 +25,30 @@ namespace arookas class sunHexLiteral : sunIntLiteral // base-16 integer { - public sunHexLiteral(sunSourceLocation location, string token) - : base(location, token) - { - - } - - protected override int ParseValue(string token) + public sunHexLiteral(sunSourceLocation location, string literal) + : base(location) { // because .NET's hex parsing is gay and doesn't support // leading signs, manually detect negative literals - var neg = (token[0] == '-'); + var neg = (literal[0] == '-'); var trim = neg ? 3 : 2; - var digits = token.Substring(trim); // trim the '0x' prefix before parsing - var value = Int32.Parse(token.Substring(2), NumberStyles.AllowHexSpecifier); + var digits = literal.Substring(trim); // trim the '0x' prefix before parsing + Value = Int32.Parse(literal.Substring(2), NumberStyles.AllowHexSpecifier); if (neg) { - value = -value; + Value = -Value; } - return value; } } class sunFloatLiteral : sunToken { - public sunFloatLiteral(sunSourceLocation location, string token) - : base(location, token) + public sunFloatLiteral(sunSourceLocation location, string literal) + : base(location) { - + Value = Single.Parse(literal); } - protected override float ParseValue(string image) { return Single.Parse(image); } - public override void Compile(sunContext context) { context.Text.PushFloat(Value); @@ -62,14 +57,12 @@ namespace arookas class sunStringLiteral : sunToken { - public sunStringLiteral(sunSourceLocation location, string token) - : base(location, token) + public sunStringLiteral(sunSourceLocation location, string literal) + : base(location) { - + Value = UnescapeString(literal.Substring(1, literal.Length - 2)); // remove enclosing quotes } - protected override string ParseValue(string image) { return UnescapeString(image.Substring(1, image.Length - 2)); } // remove enclosing quotes - public override void Compile(sunContext context) { context.Text.PushData(context.DataTable.Add(Value)); @@ -162,16 +155,17 @@ namespace arookas } } - class sunIdentifier : sunRawToken + class sunIdentifier : sunToken { - public sunIdentifier(sunSourceLocation location, string token) - : base(location, token) + public sunIdentifier(sunSourceLocation location, string identifier) + : base(location) { // make sure it is a valid identifier name (i.e. not a keyword) - if (sunParser.IsKeyword(Value)) + if (sunParser.IsKeyword(identifier)) { throw new sunIdentifierException(this); } + Value = identifier; } // identifiers are compiled on a per-context basis (i.e. at a higher level) @@ -179,23 +173,19 @@ namespace arookas class sunTrue : sunIntLiteral { - public sunTrue(sunSourceLocation location, string token) - : base(location, token) + public sunTrue(sunSourceLocation location) + : base(location) { - + Value = 1; } - - protected override int ParseValue(string token) { return 1; } } class sunFalse : sunIntLiteral { - public sunFalse(sunSourceLocation location, string token) - : base(location, token) + public sunFalse(sunSourceLocation location) + : base(location) { - + Value = 0; } - - protected override int ParseValue(string token) { return 0; } } } diff --git a/ssc/parser.cs b/ssc/parser.cs index 7d41bd6..2a30cae 100644 --- a/ssc/parser.cs +++ b/ssc/parser.cs @@ -116,8 +116,8 @@ namespace arookas case __sunConstants.STRING: return new sunStringLiteral(location, token); case __sunConstants.IDENTIFIER: return new sunIdentifier(location, token); case __sunConstants.ELLIPSIS: return new sunEllipsis(location); - case __sunConstants.TRUE: return new sunTrue(location, token); - case __sunConstants.FALSE: return new sunFalse(location, token); + case __sunConstants.TRUE: return new sunTrue(location); + case __sunConstants.FALSE: return new sunFalse(location); } // operators