public string ToStringTree(int level = 0) { var indent = new string(' ', level *2); if (level != 0) { indent += "| "; } string PrintChildren(params AstNode[] nodes) { string res = "\n"; foreach (var n in nodes) { res += n.ToStringTree(level + 1); } return(res); } return(indent + this switch { Int v => $"int: {v.val}\n", Binary v => $"binary: " + PrintChildren(v.a, v.b), Unary v => $"unary: " + PrintChildren(v.a), Variable v => $"var '{v.name}'\n", FunCall v => $"funCall: " + PrintChildren(v.fun, v.arg), Lambda v => $"lambda {v.param}: " + PrintChildren(v.body), Assign v => $"assign {v.name}:" + PrintChildren(v.val), Or v => $"or: " + PrintChildren(v.a, v.b), And v => $"and: " + PrintChildren(v.a, v.b), If v => $"if: " + PrintChildren(v.cond, v.t, v.f), _ => ToString() + "\n", });
public Node ExprPrimary() { //‹id› | ‹fun-call› | ‹array› | ‹lit› | (‹expr›) switch (CurrentToken) { case TokenCategory.IDENTIFIER: //var or fun-call var idToken = Expect(TokenCategory.IDENTIFIER); if (CurrentToken == TokenCategory.OPENEDPAR) { Expect(TokenCategory.OPENEDPAR); var fun = new FunCall() { AnchorToken = idToken }; fun.Add(ExprList()); Expect(TokenCategory.CLOSEDPAR); return(fun); } else { var id = new Identifier() { AnchorToken = idToken }; return(id); } case TokenCategory.OPENEDBRACKET: //array return(Arr()); case TokenCategory.OPENEDPAR: //(expr) Expect(TokenCategory.OPENEDPAR); var n = Expr(); Expect(TokenCategory.CLOSEDPAR); return(n); case TokenCategory.STRING: return(new Str() { AnchorToken = Expect(TokenCategory.STRING) }); case TokenCategory.CHAR: return(new Char() { AnchorToken = Expect(TokenCategory.CHAR) }); case TokenCategory.INTLITERAL: return(new IntLiteral() { AnchorToken = Expect(TokenCategory.INTLITERAL) }); default: throw new SyntaxError(firstOfExprPrimary, tokenStream.Current); } }
public void CallVisitor(FunCall funCall) { CGExpr[] expressions = new CGExpr[funCall.es.Length]; for (int i = 0; i < funCall.es.Length; i++) { funCall.es[i].VisitorCall(this); expressions[i] = result; } result = CGComposite.Make(funCall.function.name, expressions); }
//Side function for statement starting with id// public Node stmtId() { var stmtidN = new Node(); var myT = Expect(TokenCategory.IDENTIFIER); var funky = new FunCall() { AnchorToken = myT }; stmtidN = new Identifier() { AnchorToken = myT }; switch (CurrentToken) { case TokenCategory.ASSIGN: Expect(TokenCategory.ASSIGN); stmtidN = new Assign() { AnchorToken = myT }; stmtidN.Add(Expression()); Expect(TokenCategory.SEMICOLON); break; case TokenCategory.INCREASE: Expect(TokenCategory.INCREASE); stmtidN = new Increase() { AnchorToken = myT }; Expect(TokenCategory.SEMICOLON); break; case TokenCategory.DECREASE: Expect(TokenCategory.DECREASE); stmtidN = new Decrease() { AnchorToken = myT }; Expect(TokenCategory.SEMICOLON); break; case TokenCategory.PARENTHESIS_OPEN: stmtidN = funky; Expect(TokenCategory.PARENTHESIS_OPEN); stmtidN.Add(ExpressionList()); Expect(TokenCategory.PARENTHESIS_CLOSE); Expect(TokenCategory.SEMICOLON); break; } return(stmtidN); }
private void PowFactor(out Expr e) { Expr e2; Factor(out e); while (la.kind == 29) { Get(); Factor(out e2); e = FunCall.Make("^", new Expr[] { e, e2 }); } }
private void Term(out Expr e) { Expr e2; String op; PowFactor(out e); while (la.kind == 32 || la.kind == 33) { MulOp(out op); PowFactor(out e2); e = FunCall.Make(op, new Expr[] { e, e2 }); } }
private void LogicalTerm(out Expr e) { Expr e2; String op; e = null; Term(out e); while (la.kind == 17 || la.kind == 18 || la.kind == 19) { AddOp(out op); Term(out e2); e = FunCall.Make(op, new Expr[] { e, e2 }); } }
private void Expr(out Expr e) { Expr e2; String op; e = null; LogicalTerm(out e); while (StartOf(1)) { LogicalOp(out op); LogicalTerm(out e2); e = FunCall.Make(op, new Expr[] { e, e2 }); } }
//<funcall>// public string Visit(FunCall node) { String currentFunc = node.AnchorToken.Lexeme; var sb = new StringBuilder(); var functionAttr = functionsT[currentFunc]; sb.Append(VisitChildren(node)); if (functionAttr.p_or_u.Equals("p")) { sb.Append("\t\tcall int32 class ['deeplingolib']'DeepLingo'.'Utils'::'" + char.ToUpper(currentFunc[0]) + currentFunc.Substring(1) + "'"); } else { sb.Append("\t\tcall int32 class 'DeepLingoProgram'::'" + currentFunc + "'"); } sb.Append("("); if (functionAttr.arity > 0) { for (int ue = 0; ue < functionAttr.arity - 1; ue++) { sb.Append("int32, "); } sb.Append("int32"); } sb.Append(")\n"); if (functionAttr.p_or_u.Equals("p")) { if (currentFunc.Equals("printi") || currentFunc.Equals("printc") || currentFunc.Equals("prints") || currentFunc.Equals("println") || currentFunc.Equals("add") || currentFunc.Equals("set")) { sb.Append("\t\tpop\n"); } } else { if (!haScoolReturn.ContainsKey(currentFunc)) { sb.Append("\t\tpop\n"); } } return(sb.ToString()); }
// Register SDFs (and maybe later: convert DELAY calls to DelayCell). private void RegisterSdfs(Sheet sheet, int col, int row) { Cell cell = sheet[col, row]; if (cell == null || !(cell is Formula)) { return; } Expr e = (cell as Formula).Expr; if (!(e is FunCall)) { return; } FunCall funCall = e as FunCall; Expr[] es = funCall.es; switch (funCall.function.name) { case "DEFINE": if (es.Length >= 2 && es[0] is TextConst && es[1] is CellRef) { String sdfName = ((TextConst)es[0]).value.value; FullCellAddr outputCell = ((CellRef)es[1]).GetAbsoluteAddr(sheet, col, row); FullCellAddr[] inputCells = new FullCellAddr[es.Length - 2]; bool ok = true; for (int i = 2; ok && i < es.Length; i++) { CellRef inputCellRef = es[i] as CellRef; ok = inputCellRef != null; if (ok) { inputCells[i - 2] = inputCellRef.GetAbsoluteAddr(sheet, col, row); } } if (ok) { Funcalc.SdfManager.Register(outputCell, inputCells, sdfName); } } break; case "DELAY": break; } }
//----------------------------------------------------------- //<funcall>// public void Visit(FunCall node, char i) { var variableName = node.AnchorToken.Lexeme; var fctemp = FunMethods[currentFunc]; if (!FunMethods.Contains(variableName) && !FunctionTable.Contains(variableName)) { throw new SemanticError( "Func Not declared: " + variableName, node.AnchorToken); } else { if (i.Equals('c')) { parameterCounter2 = 0; VisitChildren(node, 'q'); GFuncStruct temp = FunctionTable[variableName]; //Console.WriteLine(variableName); if (parameterCounter2 != temp.arity) { throw new SemanticError( "Incorrect parameters arity in function call: " + variableName + ", expected:" + temp.arity + ", actual:" + parameterCounter2, node.AnchorToken); } } else { parameterCounter = 0; VisitChildren(node, 'c'); GFuncStruct temp = FunctionTable[variableName]; if (parameterCounter != temp.arity) { throw new SemanticError( "Incorrect parameters arity in function call: " + variableName + ", expected:" + temp.arity + ", actual:" + parameterCounter, node.AnchorToken); } } } }
public void Visit(FunCall node) { var varName = node.AnchorToken.Lexeme; if (!Global_Function_Table.Contains(varName)) { throw new SemanticError("Undeclared function: " + varName, node.AnchorToken); } else { var t = 0; foreach (var n in node[0]) { t++; } if (t != Global_Function_Table[varName]) { throw new SemanticError("Function called with different parameters as defined ", node.AnchorToken); } VisitChildren(node); } }
private void Application(out Expr e) { String s; Expr[] es; e = null; Name(out s); Expect(27); if (la.kind == 28) { Get(); e = FunCall.Make(s.ToUpper(), new Expr[0]); } else if (StartOf(4)) { Exprs1(out es); Expect(28); e = FunCall.Make(s.ToUpper(), es); } else { SynErr(39); } }
public int[,] makeFunc(FunCall input) { tempResult = new List<int>(); List<List<int>> temp = makeFuncHelper(input, true); int[][] tempArray = temp.Select(l => l.ToArray()).ToArray(); int[,] result = new int[tempArray.Count(), 4]; for (int i = 0; i < tempArray.Count(); i++) { for (int x = 0; x < 4; x++) { result[i, x] = tempArray[i][x]; } } return result; }
private List<List<int>> makeFuncHelper(FunCall input, bool root) { List<List<int>> temp = new List<List<int>>(); int locationOne = 0, locationTwo = 0; // used to hold the temp locating of the result // find where to place output bool oneIsFunc = (input.es[0] is FunCall); bool oneIsNumber = (input.es[0] is NumberConst); bool twoIsFunc = (input.es[1] is FunCall); bool twoIsNumber = (input.es[1] is NumberConst); if (oneIsFunc) { temp.AddRange(makeFuncHelper(input.es[0] as FunCall,false)); locationOne = temp[temp.Count - 1][3]; } else if(oneIsNumber) { locationOne = (int)Value.ToDoubleOrNan((input.es[0] as NumberConst).value); //TODO: ved ikke om der en nemmerer måde at gøre det her på } else { // some kind of error... } if (twoIsFunc) { temp.AddRange(makeFuncHelper(input.es[1] as FunCall, false)); locationTwo = temp[temp.Count - 1][3]; } else if (twoIsNumber) { locationTwo = (int)Value.ToDoubleOrNan((input.es[1] as NumberConst).value); //TODO: ved ikke om der en nemmerer måde at gøre det her på } else { // some kind of error... } int functionValue = 0; ; string function = input.function.name.ToString(); switch (function) { case "+": functionValue = 1; break; case "-": functionValue = 2; break; case "*": functionValue = 3; break; case "/": functionValue = 4; break; default: // some kind of error... break; } List<int> result = new List<int>(); if (oneIsFunc) { tempResult.RemoveAt(tempResult.FindLastIndex(x => x == locationOne)); } if(twoIsFunc) { tempResult.RemoveAt(tempResult.FindLastIndex(x => x == locationTwo)); } int outputPlace = 0; if(!root) { int x = -1; while (outputPlace == 0) { if(!tempResult.Contains(x)) { outputPlace = x; } else { x--; } } } tempResult.Add(outputPlace); result.Add(locationOne); result.Add(functionValue); result.Add(locationTwo); result.Add(outputPlace); temp.Add(result); return temp; }
public virtual Result Visit(FunCall funCall) { return(default(Result)); }
readonly Func <FunCall, T> fc; public T Visit(FunCall v) { return(fc(v)); }
//<expr-primary>// public Node ExprPrimary() { var xprPri = new Node(); var funky = new Node(); switch (CurrentToken) { case TokenCategory.IDENTIFIER: var myT = Expect(TokenCategory.IDENTIFIER); funky = new FunCall() { AnchorToken = myT }; xprPri = new Identifier() { AnchorToken = myT }; if (CurrentToken == TokenCategory.PARENTHESIS_OPEN) { xprPri = funky; Expect(TokenCategory.PARENTHESIS_OPEN); xprPri.Add(ExpressionList()); Expect(TokenCategory.PARENTHESIS_CLOSE); } break; case TokenCategory.BRACKET_OPEN: Expect(TokenCategory.BRACKET_OPEN); xprPri = ExpressionList("Array"); Expect(TokenCategory.BRACKET_CLOSE); break; case TokenCategory.STR_LITERAL: xprPri = new StringLiteral() { AnchorToken = Expect(TokenCategory.STR_LITERAL) }; break; case TokenCategory.CHAR_LITERAL: xprPri = new CharLiteral() { AnchorToken = Expect(TokenCategory.CHAR_LITERAL) }; break; case TokenCategory.INT_LITERAL: xprPri = new IntLiteral() { AnchorToken = Expect(TokenCategory.INT_LITERAL) }; break; case TokenCategory.PARENTHESIS_OPEN: Expect(TokenCategory.PARENTHESIS_OPEN); xprPri = Expression(); Expect(TokenCategory.PARENTHESIS_CLOSE); break; default: throw new SyntaxError(firstExprPrimary, tokenStream.Current); } return(xprPri); }
private void Factor(out Expr e) { RARef r1, r2; Sheet s1 = null; double d; bool sheetError = false; e = null; switch (la.kind) { case 1: { Application(out e); break; } case 4: case 5: case 6: case 7: case 8: case 9: case 10: case 11: case 12: case 13: case 14: { if (StartOf(2)) { } else { Get(); s1 = workbook[t.val.Substring(0, t.val.Length - 1)]; if (s1 == null) { sheetError = true; } } Raref(out r1); if (StartOf(3)) { if (sheetError) { e = new Error(ErrorValue.refError); } else { e = new CellRef(s1, r1); } } else if (la.kind == 26) { Get(); Raref(out r2); if (sheetError) { e = new Error(ErrorValue.refError); } else { e = new CellArea(s1, r1, r2); } } else { SynErr(37); } break; } case 2: { Number(out d); e = new NumberConst(d); break; } case 18: { Get(); Factor(out e); if (e is NumberConst) { e = new NumberConst(-((NumberConst)e).value.value); } else { e = FunCall.Make("NEG", new Expr[] { e }); } break; } case 15: { Get(); e = new TextConst(t.val.Substring(1, t.val.Length - 2)); break; } case 27: { Get(); Expr(out e); Expect(28); break; } default: SynErr(38); break; } }
public string Visit(FunCall node) { switch (node.AnchorToken.Lexeme) { case "printi": return(VisitChildren(node) + Line(Indent() + "call int32 class ['deeplingolib']'DeepLingo'.'Utils'::'Printi'(int32)") + Line(Indent() + "pop")); case "printc": return(VisitChildren(node) + Line(Indent() + "call int32 class ['deeplingolib']'DeepLingo'.'Utils'::'Printc'(int32)") + Line(Indent() + "pop")); case "prints": return(VisitChildren(node) + Line(Indent() + "call int32 class ['deeplingolib']'DeepLingo'.'Utils'::'Prints'(int32)") + Line(Indent() + "pop")); case "println": return(VisitChildren(node) + Line(Indent() + "call int32 class ['deeplingolib']'DeepLingo'.'Utils'::'Println'()") + Line(Indent() + "pop")); case "readi": return(VisitChildren(node) + Line(Indent() + "call int32 class ['deeplingolib']'DeepLingo'.'Utils'::'Readi'()")); case "reads": return(VisitChildren(node) + Line(Indent() + "call int32 class ['deeplingolib']'DeepLingo'.'Utils'::'Reads'()")); case "new": return(VisitChildren(node) + Line(Indent() + "call int32 class ['deeplingolib']'DeepLingo'.'Utils'::'New'(int32)")); case "size": return(VisitChildren(node) + Line(Indent() + "call int32 class ['deeplingolib']'DeepLingo'.'Utils'::'Size'(int32)")); case "add": return(VisitChildren(node) + Line(Indent() + "call int32 class ['deeplingolib']'DeepLingo'.'Utils'::'Add'(int32,int32)") + Line(Indent() + "pop")); case "get": return(VisitChildren(node) + Line(Indent() + "call int32 class ['deeplingolib']'DeepLingo'.'Utils'::'Get'(int32,int32)")); case "set": return(VisitChildren(node) + Line(Indent() + "call int32 class ['deeplingolib']'DeepLingo'.'Utils'::'Set'(int32,int32,int32)") + Line(Indent() + "pop")); case "pow": return(VisitChildren(node) + Line(Indent() + "call int32 class ['deeplingolib']'DeepLingo'.'Utils'::'Pow'(int32,int32)")); default: var temp2 = ""; if (addpop) { addpop = false; temp2 = Line(Indent() + "pop"); } var temp = ""; var result = ""; foreach (var n in node[0]) { temp += "int32,"; result += Visit((dynamic)n); } var t = result + Line(Indent() + "call int32 class 'DeepLingoProgram'::'" + node.AnchorToken.Lexeme + "'(" + temp.Substring(0, temp.Length - 1) + ")") + temp2; return(t); } }
public void CallVisitor(FunCall funCall) { CGExpr[] expressions = new CGExpr[funCall.es.Length]; for (int i = 0; i < funCall.es.Length; i++) { funCall.es[i].VisitorCall(this); expressions[i] = result; } result = CGComposite.Make(funCall.function.name, expressions); }
public Node Stmt() { switch (CurrentToken) { case TokenCategory.IDENTIFIER: var idToken = Expect(TokenCategory.IDENTIFIER); switch (CurrentToken) { case TokenCategory.ASSIGN: Expect(TokenCategory.ASSIGN); var ass = new Assignment() { AnchorToken = idToken }; ass.Add(Expr()); Expect(TokenCategory.SEMICOLON); return(ass); case TokenCategory.INCREMENT: Expect(TokenCategory.INCREMENT); //Expect(TokenCategory.INCREMENT); var inc = new Increment() { AnchorToken = idToken }; Expect(TokenCategory.SEMICOLON); return(inc); case TokenCategory.DECREMENT: Expect(TokenCategory.DECREMENT); var dec = new Decrement() { AnchorToken = idToken }; Expect(TokenCategory.SEMICOLON); return(dec); case TokenCategory.OPENEDPAR: Expect(TokenCategory.OPENEDPAR); var fun = new FunCall() { AnchorToken = idToken }; //if(CurrentToken != TokenCategory.CLOSEDPAR){ fun.Add(ExprList()); //} Expect(TokenCategory.CLOSEDPAR); Expect(TokenCategory.SEMICOLON); return(fun); } break; case TokenCategory.IF: return(If()); case TokenCategory.LOOP: return(Loop()); case TokenCategory.BREAK: var bre = new Stmt() { AnchorToken = Expect(TokenCategory.BREAK) }; Expect(TokenCategory.SEMICOLON); return(bre); case TokenCategory.RETURN: return(Return()); case TokenCategory.SEMICOLON: return(new Stmt() { AnchorToken = Expect(TokenCategory.SEMICOLON) }); default: throw new SyntaxError(firstOfStmt, tokenStream.Current); } throw new SyntaxError(firstOfStmt, tokenStream.Current); }