public SwitchStatement(Expression expr, CodeBody body, SourcePosition start, SourcePosition end) : base(ASTNodeType.SwitchStatement, start, end) { Expression = expr; Body = body; }
public SwitchStatement(Expression expr, CodeBody body, SourcePosition start, SourcePosition end) : base(ASTNodeType.SwitchStatement, start, end) { Expression = expr; Body = body; }
public DoUntilLoop(Expression cond, CodeBody body, SourcePosition start = null, SourcePosition end = null) : base(ASTNodeType.WhileLoop, start, end) { Condition = cond; Body = body; }
public WhileLoop(Expression cond, CodeBody body, SourcePosition start, SourcePosition end) : base(ASTNodeType.WhileLoop, start, end) { Condition = cond; Body = body; }
public PreOpDeclaration(String keyword, bool delim, CodeBody body, VariableType returnType, FunctionParameter operand, List<Specifier> specs, SourcePosition start, SourcePosition end) : base(ASTNodeType.PrefixOperator, keyword, delim, body, returnType, specs, start, end) { Operand = operand; }
public IfStatement(Expression cond, CodeBody then, SourcePosition start, SourcePosition end, CodeBody optelse = null) : base(ASTNodeType.IfStatement, start, end) { Condition = cond; Then = then; Else = optelse; }
public PreOpDeclaration(String keyword, bool delim, CodeBody body, VariableType returnType, FunctionParameter operand, List <Specifier> specs, SourcePosition start, SourcePosition end) : base(ASTNodeType.PrefixOperator, keyword, delim, body, returnType, specs, start, end) { Operand = operand; }
public IfStatement(Expression cond, CodeBody then, SourcePosition start, SourcePosition end, CodeBody optelse = null) : base(ASTNodeType.IfStatement, start, end) { Condition = cond; Then = then; Else = optelse; }
public InOpDeclaration(String keyword, int precedence, bool delim, CodeBody body, VariableType returnType, FunctionParameter leftOp, FunctionParameter rightOp, List <Specifier> specs, SourcePosition start, SourcePosition end) : base(ASTNodeType.InfixOperator, keyword, delim, body, returnType, specs, start, end) { LeftOperand = leftOp; RightOperand = rightOp; Precedence = precedence; }
public ForLoop(Expression cond, CodeBody body, Statement init, Statement update, SourcePosition start, SourcePosition end) : base(ASTNodeType.WhileLoop, start, end) { Condition = cond; Body = body; Init = init; Update = update; }
public ForLoop(Expression cond, CodeBody body, Statement init, Statement update, SourcePosition start, SourcePosition end) : base(ASTNodeType.WhileLoop, start, end) { Condition = cond; Body = body; Init = init; Update = update; }
public InOpDeclaration(String keyword, int precedence, bool delim, CodeBody body, VariableType returnType, FunctionParameter leftOp, FunctionParameter rightOp, List<Specifier> specs, SourcePosition start, SourcePosition end) : base(ASTNodeType.InfixOperator, keyword, delim, body, returnType, specs, start, end) { LeftOperand = leftOp; RightOperand = rightOp; Precedence = precedence; }
public OperatorDeclaration(ASTNodeType type, String keyword, bool delim, CodeBody body, VariableType returnType, List<Specifier> specs, SourcePosition start, SourcePosition end) : base(type, start, end) { OperatorKeyword = keyword; isDelimiter = delim; Body = body; ReturnType = returnType; Specifiers = specs; Locals = new List<VariableDeclaration>(); }
public Function(String name, VariableType returntype, CodeBody body, List <Specifier> specs, List <FunctionParameter> parameters, SourcePosition start, SourcePosition end) : base(ASTNodeType.Function, start, end) { Name = name; Body = body; ReturnType = returntype; Specifiers = specs; Parameters = parameters; Locals = new List <VariableDeclaration>(); }
public OperatorDeclaration(ASTNodeType type, String keyword, bool delim, CodeBody body, VariableType returnType, List <Specifier> specs, SourcePosition start, SourcePosition end) : base(type, start, end) { OperatorKeyword = keyword; isDelimiter = delim; Body = body; ReturnType = returnType; Specifiers = specs; Locals = new List <VariableDeclaration>(); }
public Function(String name, VariableType returntype, CodeBody body, List<Specifier> specs, List<FunctionParameter> parameters, SourcePosition start, SourcePosition end) : base(ASTNodeType.Function, start, end) { Name = name; Body = body; ReturnType = returntype; Specifiers = specs; Parameters = parameters; Locals = new List<VariableDeclaration>(); }
public State(String name, CodeBody body, List <Specifier> specs, State parent, List <Function> funcs, List <Function> ignores, List <StateLabel> labels, SourcePosition start, SourcePosition end) : base(ASTNodeType.State, start, end) { Name = name; Body = body; Specifiers = specs; Parent = parent; Functions = funcs; Ignores = ignores; Labels = labels; Locals = new List <VariableDeclaration>(); }
public State(String name, CodeBody body, List<Specifier> specs, State parent, List<Function> funcs, List<Function> ignores, List<StateLabel> labels, SourcePosition start, SourcePosition end) : base(ASTNodeType.State, start, end) { Name = name; Body = body; Specifiers = specs; Parent = parent; Functions = funcs; Ignores = ignores; Labels = labels; Locals = new List<VariableDeclaration>(); }
public State(string name, CodeBody body, StateFlags flags, State parent, List <Function> funcs, List <Function> ignores, List <Label> labels, SourcePosition start, SourcePosition end) : base(ASTNodeType.State, start, end) { Flags = flags; Name = name; Body = body; Parent = parent; Functions = funcs; Ignores = ignores; Labels = labels; if (Body != null) { Body.Outer = this; } }
public CodeBodyParser(TokenStream<String> tokens, CodeBody body, SymbolTable symbols, ASTNode containingNode, MessageLog log = null) { Log = log ?? new MessageLog(); Symbols = symbols; Tokens = tokens; _loopCount = 0; _switchCount = 0; Node = containingNode; Body = body; OuterClassScope = NodeUtils.GetOuterClassScope(containingNode); // TODO: refactor a better solution to this mess if (IsState) NodeVariables = (containingNode as State); else if (IsFunction) NodeVariables = (containingNode as Function); else if (IsOperator) NodeVariables = (containingNode as OperatorDeclaration); }
public Function(string name, FunctionFlags flags, VariableType returntype, CodeBody body, List <FunctionParameter> parameters = null, SourcePosition start = null, SourcePosition end = null) : base(ASTNodeType.Function, start, end) { Name = name; Body = body; ReturnType = returntype; Flags = flags; Parameters = parameters ?? new List <FunctionParameter>(); Locals = new List <VariableDeclaration>(); VarType = new DelegateType(this) { IsFunction = true, Declaration = this }; if (Body != null) { Body.Outer = this; } }
public ForEachLoop(Expression iterator, CodeBody body, SourcePosition start, SourcePosition end) : base(ASTNodeType.ForEachLoop, start, end) { IteratorCall = iterator; Body = body; }
public State TryParseState() { Func<ASTNode> stateSkeletonParser = () => { var specs = ParseSpecifiers(GlobalLists.StateSpecifiers); if (Tokens.ConsumeToken(TokenType.State) == null) return null; var name = Tokens.ConsumeToken(TokenType.Word); if (name == null) return Error("Expected state name!", CurrentPosition, CurrentPosition.GetModifiedPosition(0, 1, 1)); var parent = TryParseParent(); if (Tokens.ConsumeToken(TokenType.LeftBracket) == null) return Error("Expected '{'!", CurrentPosition, CurrentPosition.GetModifiedPosition(0, 1, 1)); List<Function> ignores = new List<Function>(); if (Tokens.ConsumeToken(TokenType.Ignores) != null) { do { VariableIdentifier variable = TryParseVariable(); if (variable == null) return Error("Malformed ignore statement!", CurrentPosition, CurrentPosition.GetModifiedPosition(0, 1, 1)); ignores.Add(new Function(variable.Name, null, null, null, null, variable.StartPos, variable.EndPos)); } while (Tokens.ConsumeToken(TokenType.Comma) != null); if (Tokens.ConsumeToken(TokenType.SemiColon) == null) return Error("Expected semi-colon!", CurrentPosition, CurrentPosition.GetModifiedPosition(0, 1, 1)); } var funcs = new List<Function>(); Function func = TryParseFunction(); while (func != null) { funcs.Add(func); func = TryParseFunction(); } var bodyStart = Tokens.CurrentItem.StartPosition; while (Tokens.CurrentItem.Type != TokenType.RightBracket && !Tokens.AtEnd()) { Tokens.Advance(); } var bodyEnd = Tokens.CurrentItem.StartPosition; if (Tokens.ConsumeToken(TokenType.RightBracket) == null) return Error("Expected '}'!", CurrentPosition, CurrentPosition.GetModifiedPosition(0, 1, 1)); var body = new CodeBody(new List<Statement>(), bodyStart, bodyEnd); var parentState = parent != null ? new State(parent.Name, null, null, null, null, null, null, parent.StartPos, parent.EndPos) : null; return new State(name.Value, body, specs, parentState, funcs, ignores, null, name.StartPosition, name.EndPosition); }; return (State)Tokens.TryGetTree(stateSkeletonParser); }
public OperatorDeclaration TryParseOperatorDecl() { Func<ASTNode> operatorParser = () => { var specs = ParseSpecifiers(GlobalLists.FunctionSpecifiers); var token = Tokens.ConsumeToken(TokenType.Operator) ?? Tokens.ConsumeToken(TokenType.PreOperator) ?? Tokens.ConsumeToken(TokenType.PostOperator) ?? new Token<String>(TokenType.INVALID); if (token.Type == TokenType.INVALID) return null; Token<String> precedence = null; if (token.Type == TokenType.Operator) { if (Tokens.ConsumeToken(TokenType.LeftParenth) == null) return Error("Expected '('! (Did you forget to specify operator precedence?)", CurrentPosition, CurrentPosition.GetModifiedPosition(0, 1, 1)); precedence = Tokens.ConsumeToken(TokenType.IntegerNumber); if (precedence == null) return Error("Expected an integer number!", CurrentPosition, CurrentPosition.GetModifiedPosition(0, 1, 1)); if (Tokens.ConsumeToken(TokenType.RightParenth) == null) return Error("Expected ')'!", CurrentPosition, CurrentPosition.GetModifiedPosition(0, 1, 1)); } Token<String> returnType = null, name = null; var firstString = TryParseOperatorIdentifier(); if (firstString == null) return Error("Expected operator name or return type!", CurrentPosition, CurrentPosition.GetModifiedPosition(0, 1, 1)); var secondString = TryParseOperatorIdentifier(); if (secondString == null) name = firstString; else { returnType = firstString; name = secondString; } VariableType retVarType = returnType != null ? new VariableType(returnType.Value, returnType.StartPosition, returnType.EndPosition) : null; if (Tokens.ConsumeToken(TokenType.LeftParenth) == null) return Error("Expected '('!", CurrentPosition, CurrentPosition.GetModifiedPosition(0, 1, 1)); var operands = new List<FunctionParameter>(); while (CurrentTokenType != TokenType.RightParenth) { var operand = TryParseParameter(); if (operand == null) return Error("Malformed operand!", CurrentPosition, CurrentPosition.GetModifiedPosition(0, 1, 1)); operands.Add(operand); if (Tokens.ConsumeToken(TokenType.Comma) == null && CurrentTokenType != TokenType.RightParenth) return Error("Unexpected operand content!", CurrentPosition, CurrentPosition.GetModifiedPosition(0, 1, 1)); } if (token.Type == TokenType.Operator && operands.Count != 2) return Error("In-fix operators requires exactly 2 parameters!", CurrentPosition, CurrentPosition.GetModifiedPosition(0, 1, 1)); else if (token.Type != TokenType.Operator && operands.Count != 1) return Error("Post/Pre-fix operators requires exactly 1 parameter!", CurrentPosition, CurrentPosition.GetModifiedPosition(0, 1, 1)); if (Tokens.ConsumeToken(TokenType.RightParenth) == null) return Error("Expected ')'!", CurrentPosition, CurrentPosition.GetModifiedPosition(0, 1, 1)); CodeBody body = new CodeBody(null, CurrentPosition, CurrentPosition); SourcePosition bodyStart = null, bodyEnd = null; if (Tokens.ConsumeToken(TokenType.SemiColon) == null) { if (!ParseScopeSpan(TokenType.LeftBracket, TokenType.RightBracket, out bodyStart, out bodyEnd)) return Error("Malformed operator body!", CurrentPosition, CurrentPosition.GetModifiedPosition(0, 1, 1)); body = new CodeBody(null, bodyStart, bodyEnd); } // TODO: determine if operator should be a delimiter! (should only symbol-based ones be?) if (token.Type == TokenType.PreOperator) return new PreOpDeclaration(name.Value, false, body, retVarType, operands.First(), specs, name.StartPosition, name.EndPosition); else if (token.Type == TokenType.PostOperator) return new PostOpDeclaration(name.Value, false, body, retVarType, operands.First(), specs, name.StartPosition, name.EndPosition); else return new InOpDeclaration(name.Value, Int32.Parse(precedence.Value), false, body, retVarType, operands.First(), operands.Last(), specs, name.StartPosition, name.EndPosition); }; return (OperatorDeclaration)Tokens.TryGetTree(operatorParser); }
public Function TryParseFunction() { Func<ASTNode> stubParser = () => { var specs = ParseSpecifiers(GlobalLists.FunctionSpecifiers); if (Tokens.ConsumeToken(TokenType.Function) == null) return null; Token<String> returnType = null, name = null; var firstString = Tokens.ConsumeToken(TokenType.Word); if (firstString == null) return Error("Expected function name or return type!", CurrentPosition, CurrentPosition.GetModifiedPosition(0, 1, 1)); var secondString = Tokens.ConsumeToken(TokenType.Word); if (secondString == null) name = firstString; else { returnType = firstString; name = secondString; } VariableType retVarType = returnType != null ? new VariableType(returnType.Value, returnType.StartPosition, returnType.EndPosition) : null; if (Tokens.ConsumeToken(TokenType.LeftParenth) == null) return Error("Expected '('!", CurrentPosition, CurrentPosition.GetModifiedPosition(0, 1, 1)); var parameters = new List<FunctionParameter>(); while (CurrentTokenType != TokenType.RightParenth) { var param = TryParseParameter(); if (param == null) return Error("Malformed parameter!", CurrentPosition, CurrentPosition.GetModifiedPosition(0, 1, 1)); parameters.Add(param); if (Tokens.ConsumeToken(TokenType.Comma) == null && CurrentTokenType != TokenType.RightParenth) return Error("Unexpected parameter content!", CurrentPosition, CurrentPosition.GetModifiedPosition(0, 1, 1)); } if (Tokens.ConsumeToken(TokenType.RightParenth) == null) return Error("Expected ')'!", CurrentPosition, CurrentPosition.GetModifiedPosition(0, 1, 1)); CodeBody body = new CodeBody(null, CurrentPosition, CurrentPosition); SourcePosition bodyStart = null, bodyEnd = null; if (Tokens.ConsumeToken(TokenType.SemiColon) == null) { if (!ParseScopeSpan(TokenType.LeftBracket, TokenType.RightBracket, out bodyStart, out bodyEnd)) return Error("Malformed function body!", CurrentPosition, CurrentPosition.GetModifiedPosition(0, 1, 1)); body = new CodeBody(null, bodyStart, bodyEnd); } return new Function(name.Value, retVarType, body, specs, parameters, name.StartPosition, name.EndPosition); }; return (Function)Tokens.TryGetTree(stubParser); }
public ForEachLoop(Expression iterator, CodeBody body, SourcePosition start, SourcePosition end) : base(ASTNodeType.ForEachLoop, start, end) { IteratorCall = iterator; Body = body; }
public bool VisitNode(CodeBody node) { foreach (Statement s in node.Statements) //TODO: make this proper { s.AcceptVisitor(this); } return true; }
public bool VisitNode(CodeBody node) { throw new NotImplementedException(); }
public CodeBody TryParseBodyOrStatement(bool allowEmpty = false) { Func<ASTNode> bodyParser = () => { CodeBody body = null; var single = TryParseInnerStatement(); if (single != null) { var content = new List<Statement>(); content.Add(single); body = new CodeBody(content, single.StartPos, single.EndPos); } else { body = TryParseBody(); } if (body == null) { if (allowEmpty && Tokens.ConsumeToken(TokenType.SemiColon) != null) { body = new CodeBody(null, CurrentPosition.GetModifiedPosition(0, -1, -1), CurrentPosition); } else return Error("Expected a code body or single statement!", CurrentPosition, CurrentPosition.GetModifiedPosition(0, 1, 1)); } return body; }; return (CodeBody)Tokens.TryGetTree(bodyParser); }