public VarFormalParameter(Identifier identifier, TypeDenoter type, SourcePosition position) : base(position) { _identifier = identifier; _type = type; }
/** * Creates a small AST to represent the declaration of a standard type. * * @param id * the name of the type * @param typedenoter * the type of the declaration * @return a TypeDeclaration for the given identifier to the given type */ static TypeDeclaration DeclareStdType(string id, TypeDenoter typedenoter) { var ident = new Identifier(id); var binding = new TypeDeclaration(ident, typedenoter); return(binding); }
public VarDeclaration(Identifier identifier, TypeDenoter type, SourcePosition position) : base(position) { Compiler.WriteDebuggingInfo($"Creating {this.GetType().Name}"); Identifier = identifier; Type = type; }
public InitDeclaration(Identifier identifier, TypeDenoter type, Expression expression, SourcePosition position) : base(position, type) { Compiler.WriteDebuggingInfo($"Creating {this.GetType().Name}"); Identifier = identifier; Expression = expression; }
public BinaryOperatorDeclaration(Operator op, TypeDenoter firstArgument, TypeDenoter secondArgument, TypeDenoter result) : base(SourcePosition.Empty) { Operator = op; FirstArgument = firstArgument; SecondArgument = secondArgument; Result = result; }
public TypeDenoter VisitBinaryExpression(BinaryExpression ast, Void arg) { TypeDenoter e1Type = ast.LeftExpression.Visit(this, null); TypeDenoter e2Type = ast.RightExpression.Visit(this, null); Declaration binding = ast.Operation.Visit(this, null); if (binding is BinaryOperatorDeclaration bbinding) { if (bbinding.FirstArgument == StandardEnvironment.AnyType) { CheckAndReportError(e1Type.Equals(e2Type), "incompatible argument types for \"%\"", ast.Operation, ast); } else { CheckAndReportError(e1Type.Equals(bbinding.FirstArgument), "wrong argument type for \"%\"", ast.Operation, ast.LeftExpression); CheckAndReportError(e2Type.Equals(bbinding.SecondArgument), "wrong argument type for \"%\"", ast.Operation, ast.RightExpression); } ast.Type = bbinding.Result; } else { ReportUndeclaredOrError(binding, ast.Operation, "\"%\" is not a binary operator"); ast.Type = StandardEnvironment.ErrorType; } return(ast.Type); }
public object visitIdentifierPE(IdentifierPE expr, object arg) { expr.Type = null; TypeDenoter vType = (TypeDenoter)expr.visit(this, null); return(vType); }
public TypeDenoter VisitTernaryExpression(TernaryExpression ast, Void arg) { TypeDenoter e2Type = ast.LeftExpression.Visit(this, null); TypeDenoter e3Type = ast.RightExpression.Visit(this, null); TypeDenoter e1Type = ast.Condition.Visit(this, null); if (e1Type is BoolTypeDenoter) { if (e1Type.Equals(true)) { ast.Type = e2Type; } else { ast.Type = e3Type; CheckAndReportError(e2Type.Equals(e3Type), "incompatible argument type for \"%\"", ast.Type.ToString(), ast.RightExpression.Position); } } else { ReportError("\"%\" is not a valid boolean", e1Type); } return(ast.Type); }
public TypeDenoter VisitTernaryExpression(TernaryExpression ast, Void arg) { TypeDenoter e1Type = ast.Condition.Visit(this, null); TypeDenoter e2Type = ast.LeftExpression.Visit(this, null); TypeDenoter e3Type = ast.RightExpression.Visit(this, null); if (e1Type == StandardEnvironment.BooleanType) { if (e2Type == e3Type) { ast.Type = StandardEnvironment.AnyType; } else { CheckAndReportError(e2Type.Equals(e3Type), "ternary expression member \"%\" must have the same type", ast.LeftExpression); CheckAndReportError(e3Type.Equals(e2Type), "ternary expression member \"%\" must have the same type", ast.RightExpression); ast.Type = StandardEnvironment.ErrorType; } } else { CheckAndReportError(e1Type.Equals(StandardEnvironment.BooleanType), "argument type \"%\" is not a boolean", ast.Condition); ast.Type = StandardEnvironment.ErrorType; } return(ast.Type); }
public UnaryOperatorDeclaration(Operator op, TypeDenoter argument, TypeDenoter result) : base(SourcePosition.Empty) { Operator = op; Argument = argument; Result = result; }
public FuncDeclaration(Identifier identifier, FormalParameterSequence formals, TypeDenoter type, Expression expression, SourcePosition position) : base(position) { Identifier = identifier; Formals = formals; Type = type; Expression = expression; }
/** * Creates a small AST to represent the declaration of a binary operator. This * declaration summarizes the operator's type information. * * @param op * the spelling of the operator * @param arg1Type * the type of the first argument of the binary operator * @param arg2Type * the type of the second argument of the binary operator * @param resultType * the type of the result * @return a BinaryOperatorDeclaration of the given operator with the given * argument types and returning the given result type */ static BinaryOperatorDeclaration DeclareStdBinaryOp(string op, TypeDenoter arg1Type, TypeDenoter arg2Type, TypeDenoter resultType) { var opAst = new Operator(op, SourcePosition.Empty); var binding = new BinaryOperatorDeclaration(opAst, arg1Type, arg2Type, resultType); return(binding); }
/** * Creates a small AST to represent the declaration of a unary operator. This * declaration summarizes the operator's type information. * * @param op * the spelling of the operator * @param argType * the type of the argument of the unary operator * @param resultType * the type of the result * @return a UnaryOperatorDeclaration of the given operator with the given * argument type and returning the given result type */ static UnaryOperatorDeclaration DeclareStdUnaryOp(string op, TypeDenoter argType, TypeDenoter resultType) { var opAst = new Operator(op); var binding = new UnaryOperatorDeclaration(opAst, argType, resultType); return(binding); }
/** * Creates a small AST to represent the declaration of a standard function. * * @param id * the name of the function * @param fps * the formal parameter sequence for the function * @param resultType * the type of the function result * @return a FuncDeclaration for the given identifier with the given formal * parameters and returning the given result type */ static FuncDeclaration DeclareStdFunc(string id, FormalParameterSequence fps, TypeDenoter resultType) { var ident = new Identifier(id); var binding = new FuncDeclaration(ident, fps, resultType); return(binding); }
public FuncFormalParameter(Identifier identifier, FormalParameterSequence formals, TypeDenoter type, SourcePosition position) : base(position) { _identifier = identifier; _formals = formals; _type = type; }
public Void VisitWhileCommand(WhileCommand ast, Void arg) { System.Console.WriteLine("Visiting WhileCommand"); TypeDenoter expressionType = ast.Expression.Visit(this, null); CheckAndReportError(expressionType.Equals(StandardEnvironment.BooleanType), "expression type must be boolean", ast); return(ast.Command.Visit(this, null)); }
public Void VisitAssignCommand(AssignCommand ast, Void arg) { TypeDenoter identifierType = ast.Identifier.Visit(this, null).Type; TypeDenoter expressionType = ast.Expression.Visit(this, null); CheckAndReportError(ast.Identifier.IsVariable, "LHS of assignment is not a variable", ast.Identifier); CheckAndReportError(expressionType.Equals(identifierType), "assignment incompatibilty", ast); return(null); }
/** * 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 Void VisitWhileCommand(WhileCommand ast, Void arg) { TypeDenoter expressionType = ast.Expression.Visit(this, null); if (expressionType is BoolTypeDenoter == false) { ReportError("\"%\" is not a boolean type", ast); } ast.Command.Visit(this, null); return(null); }
public Void VisitWhileCommand(WhileCommand ast, Void arg) { TypeDenoter wHile = ast.Expression.Visit(this, null); if (!(wHile is BoolTypeDenoter)) { ReportError("\"%\" is not a procedure identifier", ast); } ast.Command.Visit(this, null); return(null); }
/** * Parses the single declaration, and constructs an AST to represent its * phrase structure. * * @return a {@link triangle.compiler.syntax.trees.declarations.Declaration} * * @throws SyntaxError * a syntactic error * */ Declaration ParseSingleDeclaration() { Compiler.WriteDebuggingInfo("Parsing Single Declaration"); Location startLocation = tokens.Current.Start; switch (tokens.Current.Kind) { case TokenKind.Const: { AcceptIt(); Identifier identifier = ParseIdentifier(); Accept(TokenKind.Is); Expression expression = ParseExpression(); SourcePosition declarationPosition = new SourcePosition(startLocation, tokens.Current.Finish); return(new ConstDeclaration(identifier, expression, declarationPosition)); } case TokenKind.Var: { AcceptIt(); Identifier identifier = ParseIdentifier(); Accept(TokenKind.Colon); TypeDenoter denoter = ParseTypeDenoter(); if (tokens.Current.Kind == TokenKind.Becomes) { AcceptIt(); Expression expression = ParseExpression(); SourcePosition declarationPosition = new SourcePosition(startLocation, tokens.Current.Finish); return(new InitDeclaration(identifier, denoter, expression, declarationPosition)); } else { SourcePosition declarationPosition = new SourcePosition(startLocation, tokens.Current.Finish); return(new VarDeclaration(identifier, denoter, declarationPosition)); } } default: { RaiseSyntacticError("\"%\" cannot start a declaration", tokens.Current); return(null); } } }
// This is just for the standard environment public Void VisitFuncDeclaration(FuncDeclaration ast, Void arg) { ast.Type = ast.Type.Visit(this, null); idTable.Enter(ast.Identifier, ast); CheckAndReportError(!ast.Duplicated, "identifier \"%\" already declared", ast.Identifier, ast); idTable.OpenScope(); ast.Formals.Visit(this, null); TypeDenoter expressionType = ast.Expression.Visit(this, null); idTable.CloseScope(); CheckAndReportError(ast.Type.Equals(expressionType), "body of function \"%\" has wrong type", ast.Identifier, ast.Expression); return(null); }
public Void VisitIfCommand(IfCommand ast, Void arg) { TypeDenoter iF = ast.Expression.Visit(this, null); if (!(iF is BoolTypeDenoter)) { ReportError("\"%\" is not a procedure identifier", ast); } ast.TrueCommand.Visit(this, null); ast.FalseCommand.Visit(this, null); return(null); }
// Actual Parameters // Always returns null. Uses the given FormalParameter. public Void VisitValueParameter(ValueParameter ast, FormalParameter arg) { TypeDenoter expressionType = ast.Expression.Visit(this, null); if (arg is ConstFormalParameter param) { CheckAndReportError(expressionType.Equals(param.Type), "wrong type for const actual parameter", ast.Expression); } else { ReportError("const actual parameter not expected here", ast); } return(null); }
public TypeDenoter VisitUnaryExpression(UnaryExpression ast, Void arg) { TypeDenoter expressionType = ast.Expression.Visit(this, null); Declaration binding = ast.Operator.Visit(this, null); if (binding is UnaryOperatorDeclaration ubinding) { CheckAndReportError(expressionType.Equals(ubinding.Argument), "wrong argument type for \"%\"", ast.Operator); ast.Type = ubinding.Result; } else { ReportUndeclaredOrError(binding, ast.Operator, "\"%\" is not a unary operator"); ast.Type = StandardEnvironment.ErrorType; } return(ast.Type); }
public TypeDenoter VisitTernaryExpression(TernaryExpression ast, Void arg) { TypeDenoter condition = ast.Condition.Visit(this, null); TypeDenoter e2Type = ast.LeftExpression.Visit(this, null); TypeDenoter e3Type = ast.RightExpression.Visit(this, null); if (condition is BoolTypeDenoter) { if (e2Type.GetType() == e3Type.GetType()) { ast.Type = e3Type; } else { CheckAndReportError(e2Type.Equals(e3Type), "icompatible arguement types for \"%\"", ast.Type.ToString(), ast.Position); } } else { ReportError("\"%\" incompatible boolean", condition); } return(ast.Type); }
protected Declaration(SourcePosition pos, TypeDenoter type) : base(pos) { Compiler.WriteDebuggingInfo($"Creating {this.GetType().Name}"); Type = type; }
public VarFormalParameter(TypeDenoter type) : this(Identifier.Empty, type, SourcePosition.Empty) { }
public FuncDeclaration(Identifier identifier, FormalParameterSequence formals, TypeDenoter type) : this(identifier, formals, type, new EmptyExpression(), SourcePosition.Empty) { }
public ConstFormalParameter(TypeDenoter typeDenoter) : this(Identifier.Empty, typeDenoter, SourcePosition.Empty) { }