void Ident(out Node node) { Expect(1); int ltype = (0 == String.Compare(t.val, "return")) ? (int)ProtoCore.PrimitiveType.kTypeReturn : (int)ProtoCore.PrimitiveType.kTypeVar; IdentifierNode var = new IdentifierNode() { // TODO Jun: Move the primitive types into a class table Value = t.val, Name = t.val, type = ltype, datatype = (ProtoCore.PrimitiveType)ltype }; node = var; }
void functioncall(out Node node) { Expect(1); IdentifierNode function = new IdentifierNode() { Value = t.val, Name = t.val }; NodeList arglist = new NodeList(); Expect(8); if (StartOf(3)) { Node argNode; expr(out argNode); arglist.Add(argNode); while (la.kind == 30) { Get(); expr(out argNode); arglist.Add(argNode); } } Expect(9); FunctionCallNode funcNode = new FunctionCallNode(); funcNode.Function = function; funcNode.FormalArguments = arglist; node = funcNode; }
void functiondecl(out Node node) { FunctionDefinitionNode funcDecl = new FunctionDefinitionNode(); ProtoCore.Type rtype = new ProtoCore.Type(); rtype.Name = "var"; rtype.UID = 0; Expect(18); Expect(1); funcDecl.Name = t.val; if (la.kind == 36) { Get(); ReturnType(out rtype); } funcDecl.ReturnType = rtype; Expect(8); if (la.kind == 1 || la.kind == 25) { ArgumentSignatureNode args = new ArgumentSignatureNode(); Node argdecl; ArgDecl(out argdecl); args.AddArgument(argdecl as VarDeclNode); while (la.kind == 30) { Get(); ArgDecl(out argdecl); args.AddArgument(argdecl as VarDeclNode); } funcDecl.Signature = args; } Expect(9); isGlobalScope = false; Expect(32); funcDecl.FunctionBody = new CodeBlockNode(); NodeList body = new NodeList(); stmtlist(out body); Expect(33); funcDecl.localVars = localVarCount; funcDecl.FunctionBody.Body = body; node = funcDecl; isGlobalScope = true; localVarCount= 0; }
void num(out Node node) { node = null; String localvalue = String.Empty; if (la.kind == 38) { Get(); localvalue = "-"; } if (la.kind == 2) { Get(); node = new IntNode() { value = localvalue + t.val }; } else if (la.kind == 3) { Get(); node = new DoubleNode() { value = localvalue + t.val }; } else SynErr(67); }
void stmt(out Node node) { node = null; if (isFunctionCall()) { functioncall(out node); Expect(16); } else if (la.kind == 1) { assignstmt(out node); } else if (la.kind == 19) { ifstmt(out node); } else if (la.kind == 22) { whilestmt(out node); } else if (la.kind == 23) { forloop(out node); } else if (la.kind == 6) { languageblock(out node); } else if (la.kind == 16) { Get(); } else SynErr(51); }
void interimfactor(out Node node) { node = null; factor(out node); while (la.kind == 41 || la.kind == 42 || la.kind == 43) { Operator op; bitop(out op); Node rhsNode; factor(out rhsNode); BinaryExpressionNode bNode = new BinaryExpressionNode(); bNode.LeftNode = node; bNode.RightNode = rhsNode; bNode.Optr = op; node = bNode; } }
void logicalexpr(out Node node) { node = null; RangeExpr(out node); while (StartOf(5)) { Operator op; relop(out op); Node rhsNode = null; RangeExpr(out rhsNode); BinaryExpressionNode bNode = new BinaryExpressionNode(); bNode.LeftNode = node; bNode.RightNode = rhsNode; bNode.Optr = op; node = bNode; } }
void arrayident(out Node node) { Ident(out node); if (la.kind == 6) { Get(); IdentifierNode var = node as IdentifierNode; node = null; if (StartOf(3)) { expr(out node); } ArrayNode array = new ArrayNode(); array.Expr = node; array.Type = null; var.ArrayDimensions = array; Expect(7); while (la.kind == 6) { Get(); node = null; if (StartOf(3)) { expr(out node); } ArrayNode array2 = new ArrayNode(); array2.Expr = node; array2.Type = null; array.Type = array2; array = array2; Expect(7); } node = var; } }
void assignstmt(out Node node) { node = null; Node lhsNode = null; if (isArrayAccess()) { arrayident(out lhsNode); } else if (la.kind == 1) { Get(); int ltype = (0 == String.Compare(t.val, "return")) ? (int)ProtoCore.PrimitiveType.kTypeReturn : (int)ProtoCore.PrimitiveType.kTypeVar; lhsNode = new ProtoImperative.AST.IdentifierNode() { Value = t.val, type = ltype, datatype = (ProtoCore.PrimitiveType)ltype }; } else SynErr(52); Expect(31); Node rhsNode; if (StartOf(3)) { expr(out rhsNode); BinaryExpressionNode bNode = new BinaryExpressionNode(); bNode.LeftNode = lhsNode; bNode.RightNode = rhsNode; bNode.Optr = Operator.assign; node = bNode; Expect(16); } else if (la.kind == 6) { languageblock(out rhsNode); BinaryExpressionNode bNode = new BinaryExpressionNode(); bNode.LeftNode = lhsNode; bNode.RightNode = rhsNode; bNode.Optr = Operator.assign; node = bNode; } else SynErr(53); }
void whilestmt(out Node node) { WhileStmtNode whileStmtNode = new WhileStmtNode(); NodeList body = null; Expect(22); Expect(8); expr(out node); whileStmtNode.Expr = node; Expect(9); Expect(32); stmtlist(out body); whileStmtNode.Body = body; Expect(33); node = whileStmtNode; }
void ArgDecl(out Node node) { IdentifierNode tNode = null; VarDeclNode varDeclNode = new ProtoImperative.AST.VarDeclNode(); varDeclNode.memregion = ProtoCore.DSASM.MemoryRegion.kMemStack; if (la.kind == 25) { Get(); varDeclNode.memregion = ProtoCore.DSASM.MemoryRegion.kMemHeap; } if (isArrayAccess()) { arrayident(out node); tNode = node as IdentifierNode; varDeclNode.NameNode = tNode; } else if (la.kind == 1) { Get(); tNode = new IdentifierNode() { Value = t.val, Name = t.val, type = (int)ProtoCore.PrimitiveType.kTypeVar, datatype = ProtoCore.PrimitiveType.kTypeVar }; varDeclNode.NameNode = tNode; } else SynErr(68); ProtoCore.Type argtype = new ProtoCore.Type(); argtype.Name = "var"; argtype.rank = 0; argtype.UID = 0; if (la.kind == 36) { Get(); Expect(1); argtype.Name = t.val; if (la.kind == 6) { argtype.IsIndexable = true; Get(); Expect(7); argtype.rank = 1; if (la.kind == 6 || la.kind == 17 || la.kind == 31) { if (la.kind == 17) { Get(); Expect(6); Expect(7); argtype.rank = ProtoCore.DSASM.Constants.nDimensionArrayRank; } else { while (la.kind == 6) { Get(); Expect(7); argtype.rank++; } } } } } varDeclNode.ArgumentType = argtype; if (la.kind == 31) { Get(); Node rhsNode; expr(out rhsNode); BinaryExpressionNode bNode = new BinaryExpressionNode(); bNode.LeftNode = tNode; bNode.RightNode = rhsNode; bNode.Optr = Operator.assign; varDeclNode.NameNode = bNode; } node = varDeclNode; if(!isGlobalScope) { localVarCount++; } }
void unaryexpr(out Node node) { node = null; UnaryOperator op; Node exprNode; unaryop(out op); expr(out exprNode); UnaryExpressionNode unary = new UnaryExpressionNode(); unary.Operator = op; unary.Expression = exprNode; node = unary; }
void TernaryOp(ref Node node) { InlineConditionalNode inlineConNode = new InlineConditionalNode(); Expect(35); inlineConNode.ConditionExpression = node; node = null; expr(out node); inlineConNode.TrueExpression = node; Expect(36); node = null; expr(out node); inlineConNode.FalseExpression = node; node = inlineConNode; }
void term(out Node node) { node = null; interimfactor(out node); while (la.kind == 39 || la.kind == 40) { Operator op; mulop(out op); Node rhsNode; interimfactor(out rhsNode); BinaryExpressionNode bNode = new BinaryExpressionNode(); bNode.LeftNode = node; bNode.RightNode = rhsNode; bNode.Optr = op; node = bNode; } }
void identifierList(out Node node) { node = null; NameReference(out node); while (la.kind == 37) { Get(); Node rnode = null; NameReference(out rnode); IdentifierListNode bnode = new IdentifierListNode(); bnode.LeftNode = node; bnode.Optr = Operator.dot; bnode.RightNode = rnode; node = bnode; } }
void binexpr(out Node node) { node = null; logicalexpr(out node); while (la.kind == 44 || la.kind == 45) { Operator op; logicalop(out op); Node rhsNode = null; expr(out rhsNode); BinaryExpressionNode bNode = new BinaryExpressionNode(); bNode.LeftNode = node; bNode.RightNode = rhsNode; bNode.Optr = op; node = bNode; } }
void ifstmt(out Node node) { IfStmtNode ifStmtNode = new IfStmtNode(); NodeList body = null; Expect(19); Expect(8); expr(out node); ifStmtNode.IfExprNode = node; Expect(9); if (StartOf(2)) { Node singleStmt; stmt(out singleStmt); ifStmtNode.IfBody.Add(singleStmt); } else if (la.kind == 32) { Get(); stmtlist(out body); ifStmtNode.IfBody = body; Expect(33); } else SynErr(54); while (la.kind == 20) { ElseIfBlock elseifBlock = new ElseIfBlock(); Get(); Expect(8); expr(out node); elseifBlock.Expr = node; Expect(9); if (StartOf(2)) { Node singleStmt = null; stmt(out singleStmt); elseifBlock.Body.Add(singleStmt); } else if (la.kind == 32) { Get(); stmtlist(out body); elseifBlock.Body = body; Expect(33); } else SynErr(55); ifStmtNode.ElseIfList.Add(elseifBlock); } if (la.kind == 21) { Get(); if (StartOf(2)) { Node singleStmt = null; stmt(out singleStmt); ifStmtNode.ElseBody.Add(singleStmt); } else if (la.kind == 32) { Get(); stmtlist(out body); ifStmtNode.ElseBody = body; Expect(33); } else SynErr(56); } node = ifStmtNode; }
void expr(out Node node) { node = null; if (la.kind == 47 || la.kind == 48) { unaryexpr(out node); } else if (StartOf(4)) { binexpr(out node); } else SynErr(58); while (la.kind == 35) { TernaryOp(ref node); } }
void languageblock(out Node node) { node = null; LanguageBlockNode langblock = new LanguageBlockNode(); Expect(6); Expect(1); if( 0 == t.val.CompareTo("Imperative")) { langblock.codeblock.language = ProtoCore.Language.kImperative; } else if( 0 == t.val.CompareTo("Associative")) { langblock.codeblock.language = ProtoCore.Language.kAssociative; } while (la.kind == 30) { Get(); string key; Expect(1); key = t.val; Expect(31); Expect(4); if ("fingerprint" == key) { langblock.codeblock.fingerprint = t.val; langblock.codeblock.fingerprint = langblock.codeblock.fingerprint.Remove(0,1); langblock.codeblock.fingerprint = langblock.codeblock.fingerprint.Remove(langblock.codeblock.fingerprint.Length-1,1); } else if ("version" == key) { langblock.codeblock.version = t.val; langblock.codeblock.version = langblock.codeblock.version.Remove(0,1); langblock.codeblock.version = langblock.codeblock.version.Remove(langblock.codeblock.version.Length-1,1); } } Expect(7); Expect(26); langblock.codeblock.body = t.val.Substring(2,t.val.Length-4); node = langblock; ParseLanguageBlockNode(langblock); }
void factor(out Node node) { node = null; if (la.kind == 2 || la.kind == 3 || la.kind == 38) { num(out node); } else if (isFunctionCall()) { functioncall(out node); } else if (isArrayAccess()) { arrayident(out node); } else if (la.kind == 27) { Get(); node = new BooleanNode() { value = ProtoCore.DSASM.Literal.True }; } else if (la.kind == 28) { Get(); node = new BooleanNode() { value = ProtoCore.DSASM.Literal.False }; } else if (la.kind == 29) { Get(); node = new NullNode(); } else if (la.kind == 32) { Get(); ExprListNode exprlist = new ExprListNode(); if (StartOf(3)) { expr(out node); exprlist.list.Add(node); while (la.kind == 30) { Get(); expr(out node); exprlist.list.Add(node); } } Expect(33); node = exprlist; } else if (la.kind == 8) { Get(); expr(out node); Expect(9); } else if (la.kind == 1) { identifierList(out node); } else SynErr(65); }
void NameReference(out Node node) { node = null; if (isFunctionCall()) { functioncall(out node); } else if (la.kind == 1) { arrayident(out node); } else SynErr(59); }
void forloop(out Node forloop) { Node node; ForLoopNode loopNode = new ForLoopNode(); NodeList body = null; Expect(23); Expect(8); arrayident(out node); loopNode.id = node; Expect(34); expr(out node); loopNode.expression = node; Expect(9); if (StartOf(2)) { Node singleStmt = null; stmt(out singleStmt); loopNode.body.Add(singleStmt); } else if (la.kind == 32) { Get(); stmtlist(out body); loopNode.body = body; Expect(33); } else SynErr(57); forloop = loopNode; }
void RangeExpr(out Node node) { rel(out node); if (la.kind == 17) { RangeExprNode rnode = new RangeExprNode(); rnode.FromNode = node; Get(); rel(out node); rnode.ToNode = node; if (la.kind == 17) { RangeStepOperator op; Get(); rangeStepOperator(out op); rnode.stepoperator = op; rel(out node); rnode.StepNode = node; } node = rnode; } }
void rel(out Node node) { node = null; term(out node); while (la.kind == 38 || la.kind == 46) { Operator op; addop(out op); Node rhsNode; term(out rhsNode); BinaryExpressionNode bNode = new BinaryExpressionNode(); bNode.LeftNode = node; bNode.RightNode = rhsNode; bNode.Optr = op; node = bNode; } }