public override void Visit(VariableDCLNode node) { Console.WriteLine(node.Type + " " + node.ID); i += 2; VisitChildren(node, Inden()); i -= 2; }
public void DuplicateVariableDeclarationErrorTest() { VariableDCLNode dclNode1 = new VariableDCLNode("int", "var1", new IntNode(10)); VariableDCLNode dclNode2 = new VariableDCLNode("int", "var1", new IntNode(20)); SymbolTable symbolTable = new SymbolTable(); TypeCheckSymbolTableVisitor visitor = new TypeCheckSymbolTableVisitor(symbolTable); visitor.Visit(dclNode1); Assert.ThrowsException <VariableAlreadyDeclaredException>(() => { visitor.Visit(dclNode2); }); }
public void AssigningDoubleToIntVariable() { //Arrange ExpressionNode exprNode = new DoubleNode(25.5); VariableDCLNode node = new VariableDCLNode("int", "a", exprNode); //Act SymbolTable symbolTable = new SymbolTable(); TypeCheckSymbolTableVisitor visitor = new TypeCheckSymbolTableVisitor(symbolTable); //Assert Assert.ThrowsException <VariableInitialisationException>(() => { visitor.Visit(node); }); }
// --- Declarations --- public override void Visit(VariableDCLNode node) { if (SymbolTable.Retrieve(node.ID) is null) { Emit(node.Type + " " + node.ID + " = "); base.Visit(node); Emit(";\n"); } else { GlobalScope = true; Emit("static " + node.Type + " " + node.ID + ";\n"); GlobalScope = false; Emit(node.ID + " = "); base.Visit(node); Emit(";\n"); } }
// Declaration public override void Visit(VariableDCLNode node) { if (SymbolTable.Retrieve(node.ID) is null) { SymbolTable.Insert(node.ID, node.Type, false); } else { throw new VariableAlreadyDeclaredException(node, $"The variable \"{node.ID}\" could not be declared, as it has already been declared in the current or parent scope."); } base.Visit(node); if (node.Type == node.Init.Type || node.Type == "double" && node.Init.Type == "int") { return; } throw new VariableInitialisationException(node, "Failed to initialise in declaration. The expression has an incorrect type."); }
public override Node VisitVarDecl(ML4DParser.VarDeclContext context) { VariableDCLNode varDeclNode; switch (context.type.type.Type) { case ML4DLexer.INT: varDeclNode = new VariableDCLNode("int", context.id.Text, (ExpressionNode)Visit(context.init)); break; case ML4DLexer.DOUBLE: varDeclNode = new VariableDCLNode("double", context.id.Text, (ExpressionNode)Visit(context.init)); break; case ML4DLexer.BOOL: varDeclNode = new VariableDCLNode("bool", context.id.Text, (ExpressionNode)Visit(context.init)); break; default: throw new NotSupportedException( $"The variable {context.id.Text}, was declared with an illegal type."); } return(varDeclNode); }
public VariableAlreadyDeclaredException(VariableDCLNode node, string message) : base(message) { Node = node; }
public VariableInitialisationException(VariableDCLNode node, string message) : base(message) { Node = node; }
//-----Declaration----- public virtual void Visit(VariableDCLNode node) { VisitChildren(node); }