public void keywordsCanBeReadWhenThereIsWhitespaceAfterwards(String source, MiniPLTokenType type) { this.tokenScanner = new MiniPLTokenScanner(new Scanner(source)); dynamic token = this.tokenScanner.readNextToken(); Assert.Equal(type, token.getType()); }
private void typeCheck(INode node, MiniPLTokenType type) { if (type == MiniPLTokenType.TYPE_IDENTIFIER_INTEGER) { this.typeStack.Push(MiniPLTokenType.TYPE_IDENTIFIER_INTEGER); accessChildren(node); if (this.typeStack.Pop() != MiniPLTokenType.TYPE_IDENTIFIER_INTEGER) { throw new SemanticException("Wrong type. Expected an integer."); } } else if (type == MiniPLTokenType.TYPE_IDENTIFIER_STRING) { this.typeStack.Push(MiniPLTokenType.TYPE_IDENTIFIER_STRING); accessChildren(node); if (this.typeStack.Pop() != MiniPLTokenType.TYPE_IDENTIFIER_STRING) { throw new SemanticException("Wrong type. Expected a string."); } } else if (type == MiniPLTokenType.TYPE_IDENTIFIER_BOOL) { this.typeStack.Push(MiniPLTokenType.TYPE_IDENTIFIER_BOOL); accessChildren(node); if (this.typeStack.Pop() != MiniPLTokenType.TYPE_IDENTIFIER_BOOL) { throw new SemanticException("Wrong type. Expected a bool."); } } }
public void visitVarDeclaration(VarDeclarationNode node) { IdentifierNode identifier = (IdentifierNode)node.getChildren()[0]; string variableName = identifier.getVariableName(); if (this.forLoopControlVariables.Count > 0) { throw new SemanticException("Declaring variables inside for loop is not allowed."); } if (variableAlreadyDeclared(variableName)) { throw new SemanticException("Variable '" + variableName + "' already declared."); } TypeNode typeNode = (TypeNode)node.getChildren()[1]; MiniPLTokenType type = (MiniPLTokenType)typeNode.getValue(); if (type == MiniPLTokenType.TYPE_IDENTIFIER_INTEGER) { this.symbolTable.addVariable(variableName, 0); } else if (type == MiniPLTokenType.TYPE_IDENTIFIER_STRING) { this.symbolTable.addVariable(variableName, ""); } else if (type == MiniPLTokenType.TYPE_IDENTIFIER_BOOL) { this.symbolTable.addVariable(variableName, false); } else { throw new Exception("Unknown type usage in semantic analyzer."); } }
public void readsSingleCharacterTokenFromSource(String source, MiniPLTokenType type) { this.tokenScanner = new MiniPLTokenScanner(new Scanner(source)); dynamic token = this.tokenScanner.readNextToken(); Assert.Equal(type, token.getType()); Assert.Equal(source, token.getLexeme()); }
public void visitVarDeclaration(VarDeclarationNode node) { IdentifierNode identifier = (IdentifierNode)node.getChildren()[0]; string variableName = identifier.getVariableName(); TypeNode typeNode = (TypeNode)node.getChildren()[1]; MiniPLTokenType type = (MiniPLTokenType)typeNode.getValue(); typeCheck(typeNode, type); }
public bool isTokenType(MiniPLTokenType type) { if (this.token == null) { return(false); } else { return(this.token.getType().Equals(type)); } }
public bool firstContains(MiniPLSymbol symbol, MiniPLTokenType tokenType) { if (firstMap.ContainsKey(symbol)) { return(firstMap[symbol].Contains(tokenType)); } else { return(false); } }
public void visitPrint(PrintNode printNode) { this.typeStack.Clear(); printNode.getChildren()[0].accept(this); MiniPLTokenType type = this.typeStack.Pop(); this.typeStack.Clear(); if (type != MiniPLTokenType.TYPE_IDENTIFIER_INTEGER && type != MiniPLTokenType.TYPE_IDENTIFIER_STRING) { throw new SemanticException("Print statement can print only integers and strings."); } }
public void visitAssert(AssertNode assertNode) { this.typeStack.Clear(); assertNode.getChildren()[0].accept(this); MiniPLTokenType type = this.typeStack.Pop(); this.typeStack.Clear(); if (type != MiniPLTokenType.TYPE_IDENTIFIER_BOOL) { throw new SemanticException("Assert statement can only take bool as an argument."); } }
public void visitLessThanOperator(LessThanOperationNode node) { accessChildren(node); MiniPLTokenType left = this.typeStack.Pop(); MiniPLTokenType right = this.typeStack.Pop(); if (left != right) { throw new SemanticException("Less than operator has different types on both sides."); } this.typeStack.Push(MiniPLTokenType.TYPE_IDENTIFIER_BOOL); }
public void visitDivision(DivisionOperationNode node) { accessChildren(node); MiniPLTokenType left = this.typeStack.Pop(); MiniPLTokenType right = this.typeStack.Pop(); if (left != right) { throw new SemanticException("Wrong type. Expected an integer."); } this.typeStack.Push(MiniPLTokenType.TYPE_IDENTIFIER_INTEGER); }
public void visitLogicalAndOperator(LogicalAndOperationNode node) { accessChildren(node); MiniPLTokenType left = this.typeStack.Pop(); MiniPLTokenType right = this.typeStack.Pop(); if (left != right) { throw new SemanticException("Logical not operator has different types on both sides. Expected bool values,"); } this.typeStack.Push(MiniPLTokenType.TYPE_IDENTIFIER_BOOL); }
public bool match(MiniPLTokenType type, string errorToken) { if (this.token == null) { throw new SyntaxException("Expected " + errorToken, token); } else { if (this.token.getType().Equals(type)) { return(true); } else { throw new SyntaxException("Expected " + errorToken + ". Found '" + token.getLexeme() + "' instead.", token); } } }
public INode makeNode(MiniPLTokenType type) { switch (type) { case MiniPLTokenType.PLUS: return(new PlusOperationNode()); case MiniPLTokenType.MINUS: return(new MinusOperationNode()); case MiniPLTokenType.ASTERISK: return(new MultiplicationOperationNode()); case MiniPLTokenType.SLASH: return(new DivisionOperationNode()); case MiniPLTokenType.LOGICAL_NOT: return(new LogicalNotOperationNode()); case MiniPLTokenType.LOGICAL_AND: return(new LogicalAndOperationNode()); case MiniPLTokenType.EQUALITY_COMPARISON: return(new EqualityOperationNode()); case MiniPLTokenType.LESS_THAN_COMPARISON: return(new LessThanOperationNode()); case MiniPLTokenType.TYPE_IDENTIFIER_INTEGER: case MiniPLTokenType.TYPE_IDENTIFIER_STRING: case MiniPLTokenType.TYPE_IDENTIFIER_BOOL: return(new TypeNode(type)); case MiniPLTokenType.RANGE_OPERATOR: return(new RangeOperatorNode()); default: return(new DummyNode <MiniPLTokenType>(type)); } }
public void visitPlus(PlusOperationNode node) { accessChildren(node); MiniPLTokenType left = this.typeStack.Pop(); MiniPLTokenType right = this.typeStack.Pop(); if (left == MiniPLTokenType.TYPE_IDENTIFIER_STRING) { if (left != right) { throw new SemanticException("Wrong type. Expected a string."); } this.typeStack.Push(MiniPLTokenType.TYPE_IDENTIFIER_STRING); } else if (left == MiniPLTokenType.TYPE_IDENTIFIER_INTEGER) { if (left != right) { throw new SemanticException("Wrong type. Expected a string."); } this.typeStack.Push(MiniPLTokenType.TYPE_IDENTIFIER_INTEGER); } }
private bool symbolInFirst(MiniPLSymbol symbol, MiniPLTokenType miniPLTokenType) { return(this.firstAndFollow.firstContains(symbol, miniPLTokenType)); }
public Token <MiniPLTokenType> createToken(MiniPLTokenType type, String lexeme) { return(createToken(type, lexeme, rowNumber, columnNumber)); }
private Token <MiniPLTokenType> createToken(MiniPLTokenType type, String lexeme, int rowNumber, int columnNumber) { return(new Token <MiniPLTokenType>(type, lexeme, rowNumber, columnNumber - lexeme.Length + 1)); }
public bool isNextTokensType(MiniPLTokenType type) { return(this.nextToken != null && this.nextToken.getType().Equals(type)); }