Skip to content

Commit

Permalink
Use SyntaxNumber as Expression ... well, sort of ...
Browse files Browse the repository at this point in the history
  • Loading branch information
wiztigers committed Jul 7, 2015
1 parent eadf874 commit ef784cb
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 55 deletions.
13 changes: 7 additions & 6 deletions TypeCobol/Compiler/CodeElements/Expressions/Expression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace TypeCobol.Compiler.CodeElements.Expressions
{
public class Expression { }
public class ArithmeticExpression : Expression { }

public class Addition : Expression
public class Addition : ArithmeticExpression
{
public Expression left { get; set; }
public Expression right { get; set; }
Expand All @@ -29,13 +30,13 @@ public Identifier(Token token, bool rounded = false)
}
public override string ToString() { return token.Text; }
}
public class Literal : Expression
public class Number : ArithmeticExpression
{
public Token token { get; set; }
public Literal(Token token)
public SyntaxNumber number { get; set; }
public Number(SyntaxNumber number)
{
this.token = token;
this.number = number;
}
public override string ToString() { return token.Text; }
public override string ToString() { return number.ToString(); }
}
}
47 changes: 0 additions & 47 deletions TypeCobol/Compiler/CodeElements/Expressions/SyntaxInteger.cs

This file was deleted.

130 changes: 130 additions & 0 deletions TypeCobol/Compiler/CodeElements/Expressions/SyntaxNumber.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
using System;
using TypeCobol.Compiler.Scanner;

namespace TypeCobol.Compiler.CodeElements
{

/// <summary>
/// Integer, Decimal or Floating Point value defined by a single Token in the Cobol syntax
/// </summary>
public class SyntaxNumber
{
public SyntaxNumber(Token token)
{
Token = token;
}

/// <summary>
/// Token defining the number value
/// </summary>
public Token Token { get; private set; }
}

/// <summary>
/// Integer value defined by a single Token in the Cobol syntax
/// </summary>
public class SyntaxInteger : SyntaxNumber
{
public SyntaxInteger(Token token)
: base(token)
{ }

/// <summary>
/// Integer value defined by the Token
/// </summary>
public long Value
{
get
{
if (Token.TokenType == TokenType.IntegerLiteral)
{
return ((IntegerLiteralValue)Token.LiteralValue).Number;
}
else
{
throw new InvalidOperationException("An integer value can not be defined by a token of type : " + Token.TokenType);
}
}
}

/// <summary>
/// Debug string
/// </summary>
public override string ToString()
{
return Value.ToString();
}
}

/// <summary>
/// Decimal value defined by a single Token in the Cobol syntax
/// </summary>
public class SyntaxDecimal : SyntaxNumber
{
public SyntaxDecimal(Token token)
: base(token)
{ }

/// <summary>
/// Integer value defined by the Token
/// </summary>
public double Value
{
get
{
if (Token.TokenType == TokenType.DecimalLiteral)
{
return ((DecimalLiteralValue)Token.LiteralValue).Number;
}
else
{
throw new InvalidOperationException("A decimal value can not be defined by a token of type : " + Token.TokenType);
}
}
}

/// <summary>
/// Debug string
/// </summary>
public override string ToString()
{
return Value.ToString();
}
}

/// <summary>
/// Decimal value defined by a single Token in the Cobol syntax
/// </summary>
public class SyntaxFloat : SyntaxNumber
{
public SyntaxFloat(Token token)
: base(token)
{ }

/// <summary>
/// Integer value defined by the Token
/// </summary>
public double Value
{
get
{
if (Token.TokenType == TokenType.FloatingPointLiteral)
{
return ((DecimalLiteralValue)Token.LiteralValue).Number;
}
else
{
throw new InvalidOperationException("A floating point value can not be defined by a token of type : " + Token.TokenType);
}
}
}

/// <summary>
/// Debug string
/// </summary>
public override string ToString()
{
return Value.ToString();
}
}
}
32 changes: 31 additions & 1 deletion TypeCobol/Compiler/Parser/CodeElementBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,31 @@ public override void EnterAcceptStatement(CobolCodeElementsParser.AcceptStatemen
CodeElement = new AcceptStatement();
}


public SyntaxNumber createNumber(CobolCodeElementsParser.NumericLiteralContext context)
{
if (context.IntegerLiteral() != null)
{
Token token = ParseTreeUtils.GetTokenFromTerminalNode(context.IntegerLiteral());
return new SyntaxInteger(token);
}
if (context.DecimalLiteral() != null)
{
Token token = ParseTreeUtils.GetTokenFromTerminalNode(context.DecimalLiteral());
return new SyntaxDecimal(token);
}
if (context.FloatingPointLiteral() != null)
{
Token token = ParseTreeUtils.GetTokenFromTerminalNode(context.FloatingPointLiteral());
return new SyntaxFloat(token);
}
if (context.ZERO() != null || context.ZEROS() != null || context.ZEROES() != null)
{
throw new System.Exception("TODO!");
}
throw new System.Exception("This is not a number!");
}

private Expression createLeftOperand(IReadOnlyList<CobolCodeElementsParser.IdentifierOrLiteralContext> operands)
{
Expression left = null;
Expand All @@ -441,7 +466,12 @@ private Expression createLeftOperand(IReadOnlyList<CobolCodeElementsParser.Ident
else
if (operand.literal() != null)
{
tail = new Identifier(ParseTreeUtils.GetFirstToken(operand.literal()));
SyntaxNumber number = null;
if (operand.literal().numericLiteral() != null)
{
number = createNumber(operand.literal().numericLiteral());
tail = new Number(number);
}
}
if (tail == null) continue;
if (left == null)
Expand Down
3 changes: 2 additions & 1 deletion TypeCobol/TypeCobol.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
<Compile Include="Compiler\CodeElements\Entry\IOControlEntry.cs" />
<Compile Include="Compiler\CodeElements\Expressions\Expression.cs" />
<Compile Include="Compiler\CodeElements\Expressions\SyntaxBoolean.cs" />
<Compile Include="Compiler\CodeElements\Expressions\SyntaxInteger.cs" />
<Compile Include="Compiler\CodeElements\Expressions\SyntaxNumber.cs" />
<Compile Include="Compiler\CodeElements\Expressions\SyntaxProperty.cs" />
<Compile Include="Compiler\CodeElements\Expressions\SyntaxString.cs" />
<Compile Include="Compiler\CodeElements\Header\ConfigurationSectionHeader.cs" />
Expand Down Expand Up @@ -296,6 +296,7 @@
<Compile Include="Compiler\Generator\TypeCobolGenerator.cs" />
<Compile Include="Compiler\Parser\CodeElementBuilder.cs" />
<Compile Include="Compiler\Parser\CodeElementChangedEvent.cs" />
<Compile Include="Compiler\Parser\LiteralFactory.cs" />
<Compile Include="Compiler\Parser\ProgramClassBuilder.cs" />
<Compile Include="Compiler\Parser\SyntaxDocument.cs" />
<Compile Include="Compiler\Parser\TracingCobolParser.cs" />
Expand Down

0 comments on commit ef784cb

Please sign in to comment.