Token class cleanup.

This commit is contained in:
arookas 2015-12-27 21:05:25 -05:00
parent af2a00f225
commit 7e6c43d91e
3 changed files with 35 additions and 58 deletions

View file

@ -132,25 +132,12 @@ namespace arookas
abstract class sunToken<TValue> : sunNode abstract class sunToken<TValue> : 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) : base(location)
{ {
Value = ParseValue(token);
}
protected abstract TValue ParseValue(string token);
}
abstract class sunRawToken : sunToken<string>
{
protected sunRawToken(sunSourceLocation location, string token)
: base(location, token)
{
} }
protected override string ParseValue(string token) { return token; }
} }
} }

View file

@ -6,13 +6,16 @@ namespace arookas
{ {
class sunIntLiteral : sunToken<int> // base-10 integer class sunIntLiteral : sunToken<int> // base-10 integer
{ {
public sunIntLiteral(sunSourceLocation location, string token) public sunIntLiteral(sunSourceLocation location, string literal)
: base(location, token) : 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) public override void Compile(sunContext context)
{ {
@ -22,38 +25,30 @@ namespace arookas
class sunHexLiteral : sunIntLiteral // base-16 integer class sunHexLiteral : sunIntLiteral // base-16 integer
{ {
public sunHexLiteral(sunSourceLocation location, string token) public sunHexLiteral(sunSourceLocation location, string literal)
: base(location, token) : base(location)
{
}
protected override int ParseValue(string token)
{ {
// because .NET's hex parsing is gay and doesn't support // because .NET's hex parsing is gay and doesn't support
// leading signs, manually detect negative literals // leading signs, manually detect negative literals
var neg = (token[0] == '-'); var neg = (literal[0] == '-');
var trim = neg ? 3 : 2; var trim = neg ? 3 : 2;
var digits = token.Substring(trim); // trim the '0x' prefix before parsing var digits = literal.Substring(trim); // trim the '0x' prefix before parsing
var value = Int32.Parse(token.Substring(2), NumberStyles.AllowHexSpecifier); Value = Int32.Parse(literal.Substring(2), NumberStyles.AllowHexSpecifier);
if (neg) if (neg)
{ {
value = -value; Value = -Value;
} }
return value;
} }
} }
class sunFloatLiteral : sunToken<float> class sunFloatLiteral : sunToken<float>
{ {
public sunFloatLiteral(sunSourceLocation location, string token) public sunFloatLiteral(sunSourceLocation location, string literal)
: base(location, token) : base(location)
{ {
Value = Single.Parse(literal);
} }
protected override float ParseValue(string image) { return Single.Parse(image); }
public override void Compile(sunContext context) public override void Compile(sunContext context)
{ {
context.Text.PushFloat(Value); context.Text.PushFloat(Value);
@ -62,14 +57,12 @@ namespace arookas
class sunStringLiteral : sunToken<string> class sunStringLiteral : sunToken<string>
{ {
public sunStringLiteral(sunSourceLocation location, string token) public sunStringLiteral(sunSourceLocation location, string literal)
: base(location, token) : 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) public override void Compile(sunContext context)
{ {
context.Text.PushData(context.DataTable.Add(Value)); context.Text.PushData(context.DataTable.Add(Value));
@ -162,16 +155,17 @@ namespace arookas
} }
} }
class sunIdentifier : sunRawToken class sunIdentifier : sunToken<string>
{ {
public sunIdentifier(sunSourceLocation location, string token) public sunIdentifier(sunSourceLocation location, string identifier)
: base(location, token) : base(location)
{ {
// make sure it is a valid identifier name (i.e. not a keyword) // 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); throw new sunIdentifierException(this);
} }
Value = identifier;
} }
// identifiers are compiled on a per-context basis (i.e. at a higher level) // identifiers are compiled on a per-context basis (i.e. at a higher level)
@ -179,23 +173,19 @@ namespace arookas
class sunTrue : sunIntLiteral class sunTrue : sunIntLiteral
{ {
public sunTrue(sunSourceLocation location, string token) public sunTrue(sunSourceLocation location)
: base(location, token) : base(location)
{ {
Value = 1;
} }
protected override int ParseValue(string token) { return 1; }
} }
class sunFalse : sunIntLiteral class sunFalse : sunIntLiteral
{ {
public sunFalse(sunSourceLocation location, string token) public sunFalse(sunSourceLocation location)
: base(location, token) : base(location)
{ {
Value = 0;
} }
protected override int ParseValue(string token) { return 0; }
} }
} }

View file

@ -116,8 +116,8 @@ namespace arookas
case __sunConstants.STRING: return new sunStringLiteral(location, token); case __sunConstants.STRING: return new sunStringLiteral(location, token);
case __sunConstants.IDENTIFIER: return new sunIdentifier(location, token); case __sunConstants.IDENTIFIER: return new sunIdentifier(location, token);
case __sunConstants.ELLIPSIS: return new sunEllipsis(location); case __sunConstants.ELLIPSIS: return new sunEllipsis(location);
case __sunConstants.TRUE: return new sunTrue(location, token); case __sunConstants.TRUE: return new sunTrue(location);
case __sunConstants.FALSE: return new sunFalse(location, token); case __sunConstants.FALSE: return new sunFalse(location);
} }
// operators // operators