/// <summary> /// Parses the single command, and constructs an AST to represent its phrase /// structure. /// </summary> /// <returns> /// a {@link triangle.compiler.syntax.trees.commands.Command} /// </returns> /// <throws type="SyntaxError"> /// a syntactic error /// </throws> Command ParseSingleCommand() { Compiler.WriteDebuggingInfo("Parsing Single Command"); Location startLocation = tokens.Current.Start; switch (tokens.Current.Kind) { case TokenKind.Identifier: { Compiler.WriteDebuggingInfo("Parsing Assignment Command or Call Command"); Identifier identifier = ParseIdentifier(); if (tokens.Current.Kind == TokenKind.LeftBracket) { Compiler.WriteDebuggingInfo("Parsing Call Command"); AcceptIt(); ParameterSequence parameters = ParseParameters(); Accept(TokenKind.RightBracket); SourcePosition commandPosition = new SourcePosition(startLocation, tokens.Current.Finish); return(new CallCommand(identifier, parameters, commandPosition)); } else if (tokens.Current.Kind == TokenKind.Becomes) { Compiler.WriteDebuggingInfo("Parsing Assignment Command"); Accept(TokenKind.Becomes); Expression expression = ParseExpression(); SourcePosition commandPosition = new SourcePosition(startLocation, tokens.Current.Finish); return(new AssignCommand(identifier, expression, commandPosition)); } else { RaiseSyntacticError("Expected either an assignment or function call but found a \"%\"", tokens.Current); return(null); } } case TokenKind.Begin: { Compiler.WriteDebuggingInfo("Parsing Begin Command"); AcceptIt(); Command command = ParseCommand(); Accept(TokenKind.End); // SourcePosition commandPosition = new SourcePosition(startLocation, tokens.Current.Finish); return(command); } case TokenKind.Let: { Compiler.WriteDebuggingInfo("Parsing Let Command"); AcceptIt(); Declaration declaration = ParseDeclaration(); Accept(TokenKind.In); Command command = ParseCommand(); SourcePosition commandPosition = new SourcePosition(startLocation, tokens.Current.Finish); return(new LetCommand(declaration, command, commandPosition)); } case TokenKind.If: { Compiler.WriteDebuggingInfo("Parsing If Command"); AcceptIt(); Expression expression = ParseExpression(); Accept(TokenKind.Then); Command the = ParseSingleCommand(); Accept(TokenKind.Else); Command els = ParseSingleCommand(); SourcePosition commandPosition = new SourcePosition(startLocation, tokens.Current.Finish); return(new IfCommand(expression, the, els, commandPosition)); } case TokenKind.While: { Compiler.WriteDebuggingInfo("Parsing While Command"); AcceptIt(); Expression expression = ParseExpression(); Accept(TokenKind.Do); Command command = ParseSingleCommand(); SourcePosition commandPosition = new SourcePosition(startLocation, tokens.Current.Finish); return(new WhileCommand(expression, command, commandPosition)); } case TokenKind.Skip: { Compiler.WriteDebuggingInfo("Parsing Skip Command"); AcceptIt(); SourcePosition commandPosition = new SourcePosition(startLocation, tokens.Current.Finish); return(new SkipCommand(commandPosition)); } default: RaiseSyntacticError("\"%\" cannot start a command", tokens.Current); return(null); } }
/// Parses the primary expression, and constructs an AST to represent its phrase structure. /// @return an {@link Triangle.SyntaxTrees.Expressions.Expression} /// @throws SyntaxError private Expression ParsePrimaryExpression() { Compiler.WriteDebuggingInfo("Parsing Primary Expression"); Location startLocation = tokens.Current.Start; switch (tokens.Current.Kind) { case TokenKind.IntLiteral: { Compiler.WriteDebuggingInfo("Parsing Int Expression"); IntegerLiteral il = ParseIntegerLiteral(); SourcePosition expressionPos = new SourcePosition(startLocation, tokens.Current.Finish); return(new IntegerExpression(il, expressionPos)); } case TokenKind.CharLiteral: { Compiler.WriteDebuggingInfo("Parsing Char Expression"); CharacterLiteral cl = ParseCharacterLiteral(); SourcePosition expressionPos = new SourcePosition(startLocation, tokens.Current.Finish); return(new CharacterExpression(cl, expressionPos)); } case TokenKind.Identifier: { Compiler.WriteDebuggingInfo("Parsing Call Expression or Identifier Expression"); Identifier id = ParseIdentifier(); if (tokens.Current.Kind == TokenKind.LeftBracket) { Compiler.WriteDebuggingInfo("Parsing Call Expression"); AcceptIt(); ParameterSequence ps = ParseParameters(); Accept(TokenKind.RightBracket); SourcePosition expressionPos = new SourcePosition(startLocation, tokens.Current.Finish); return(new CallExpression(id, ps, expressionPos)); } else { Compiler.WriteDebuggingInfo("Parsing Identifier Expression"); SourcePosition expressionPos = new SourcePosition(startLocation, tokens.Current.Finish); return(new IdExpression(id, expressionPos)); } } case TokenKind.Operator: { Compiler.WriteDebuggingInfo("Parsing Unary Expression"); Operator op = ParseOperator(); Expression p_exp = ParsePrimaryExpression(); SourcePosition expressionPos = new SourcePosition(startLocation, tokens.Current.Finish); return(new UnaryExpression(op, p_exp, expressionPos)); } case TokenKind.LeftBracket: { Compiler.WriteDebuggingInfo("Parsing Bracket Expression"); AcceptIt(); Expression exp = ParseExpression(); Accept(TokenKind.RightBracket); return(exp); } default: { RaiseSyntacticError("\"%\" cannot start an expression", tokens.Current); return(null); } } }