private static VariableDeclaratorSyntax FieldConstDeclaration(ConstDeclaration constDeclaration, VariableDeclaratorSyntax varDeclarator) { var assignment = constDeclaration.Type.Type switch { BaseTypes.Int => SyntaxFactory.EqualsValueClause( SyntaxFactory.LiteralExpression( SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(constDeclaration.Value.ToInt32()))), BaseTypes.Bool => SyntaxFactory.EqualsValueClause( SyntaxFactory.LiteralExpression( constDeclaration.Value.ToBool() ? SyntaxKind.TrueLiteralExpression : SyntaxKind.FalseLiteralExpression)), BaseTypes.Real => SyntaxFactory.EqualsValueClause( SyntaxFactory.LiteralExpression( SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(constDeclaration.Value.ToDouble()))), _ => throw new ArgumentException( "Cannot handle type " + Enum.GetName(typeof(BaseTypes), constDeclaration.Type.Type), nameof(constDeclaration)) }; return(varDeclarator.WithInitializer(assignment)); }
public Void VisitConstDeclaration(ConstDeclaration ast, Void arg) { ast.Expression.Visit(this, null); idTable.Enter(ast.Identifier, ast); CheckAndReportError(!ast.Duplicated, "identifier \"%\" already declared", ast.Identifier, ast); return(null); }
// Decides run-time representation of a standard constant. void ElaborateStdConst(ConstDeclaration decl, int value) { int typeSize = decl.Expression.Type.Visit(this, null); decl.Entity = new KnownValue(typeSize, value); Encoder.WriteTableDetails(decl); }
public Type Visit(ConstDeclaration node) { var varName = node[0].AnchorToken.Lexeme; var type = Visit((dynamic)node[1]); if (localscope != null) { if (currentLocalConstTable.Contains(varName)) { throw new SemanticError("Duplicated constant: " + varName, node[0].AnchorToken); } else if (currentLocalSymbolTable.Contains(varName)) { throw new SemanticError("Constant and variable cannot have the same name: " + varName, node[0].AnchorToken); } currentLocalConstTable[varName] = type; } else { if (globalConstTable.Contains(varName)) { throw new SemanticError("Duplicated constant: " + varName, node[0].AnchorToken); } else if (globalSymbolTable.Contains(varName)) { throw new SemanticError("Constant and variable cannot have the same name: " + varName, node[0].AnchorToken); } else { globalConstTable[varName] = type; } } return(Type.VOID); }
public override bool Visit(ConstDeclaration node) { string name = node.name as string; outputCode("#define " + name + " ", false, false); traverse(node.init); outputCode("", false, true); return(true); }
/** * Creates a small AST to represent the declaration of a standard constant. * * @param id * the name of the constant * @param constType * the type of the constant declaration * @return a ConstDeclaration for the given identifier to the given type */ static ConstDeclaration DeclareStdConst(string id, TypeDenoter constType) { var ident = new Identifier(id); // constExpr used only as a placeholder for constType var constExpr = new EmptyExpression(); constExpr.Type = constType; var binding = new ConstDeclaration(ident, constExpr); return(binding); }
public Node ConstDeclaration() { var id = new Identifier() { AnchorToken = Expect(TokenCategory.IDENTIFIER) }; var result = new ConstDeclaration() { AnchorToken = Expect(TokenCategory.ASSIGN) }; result.Add(id); result.Add(Literal()); Expect(TokenCategory.SEMICOL); return(result); }
public int VisitConstDeclaration(ConstDeclaration ast, Frame frame) { var extraSize = 0; var expr = ast.Expression; if (expr.IsLiteral) { ast.Entity = new KnownValue(expr.Type.Size, expr.Value); } else { extraSize = expr.Visit(this, frame); ast.Entity = new UnknownValue(extraSize, frame); } Encoder.WriteTableDetails(ast); return(extraSize); }
public string Visit(ConstDeclaration node) { var varName = node[0].AnchorToken.Lexeme; var type = Visit((dynamic)node[1]); if (localscope != null) { try { localSymbolAssLoad.Add(varName, type); }catch { } } else { try { globalSymbolAssLoad.Add(varName, type); }catch { } } return(""); }
public int VisitConstDeclaration(ConstDeclaration ast, Frame frame) { int extraSize = 0; Expression expr = ast.Expression; switch (expr) { case CharacterExpression charExpression: ast.Entity = new KnownValue(charExpression.Type.Size, charExpression.Value); break; case IntegerExpression intExpression: ast.Entity = new KnownValue(intExpression.Type.Size, intExpression.Value); break; default: extraSize = expr.Visit(this, frame); ast.Entity = new UnknownValue(extraSize, frame); break; } Encoder.WriteTableDetails(ast); return(extraSize); }
public virtual T Visit(ConstDeclaration node) => Visit(node as Node);
public virtual T Visit(ConstDeclaration node) { Visit((ValueDeclaration)node); return(traverse(node.init)); }
public override bool Visit(ConstDeclaration node) { Visit((ValueDeclaration)node); TraversePrint(node.init); return(true); }
// Decides run-time representation of a standard constant. private void ElaborateStdConst(ConstDeclaration decl, int value) { decl.Entity = new KnownValue(decl.Expression.Type.Visit(this, null), value); WriteTableDetails(decl); }
void EnterStdDeclaration(ConstDeclaration declaration) { EnterStdDeclaration(declaration.Identifier, declaration); }