//TODO TESTS private static void Command(ListController <TokenModel> lc) { var beg = lc.GetNext(); if (TypesTools.IsType(beg.Text)) { lc.SetBack(); Initialization(lc); } else if (VarTable.Contains(beg.Text)) { lc.SetBack(); var node = VariableAssign(lc); Commands.Add(node); } else if (FuncTable.Contains(beg.Text)) { lc.SetBack(); var node = FuncExecute(lc); Commands.Add(node); } else { throw new ParseException($"Cannot resolve '{beg.Text}'", beg); } //todo lately var = Arithmetic Expression //todo more lately type func }
public LiteValue Visit(SyntaxFunctionNode Node, LiteEnv Env) { var FuncValue = FuncTable.AddFuncEx(new FuncLite(Node.GetFuncName(), Env, Node.GetParamList() as SyntaxParamListStatementNode, Node.GetBlock() as SyntaxBlockStatementNode)); Env.SetSelf(Node.GetFuncName(), FuncValue); return(FuncValue); }
public Entry() : base() { removeNthFromEnd = new RemoveNthFromEnd(); integerToRoman = new IntegerToRoman(); deleteColumnsMakeSorted = new DeleteColumnsMakeSorted(); minCoinChallenge = new MinCoinChallenge(); FuncTable.Add((int)FunctionIndex.RemoveNthFromEnd, removeNthFromEnd); FuncTable.Add((int)FunctionIndex.IntegerToRoman, integerToRoman); FuncTable.Add((int)FunctionIndex.DeleteColumnsMakeSorted, deleteColumnsMakeSorted); FuncTable.Add((int)FunctionIndex.MinCoinChallenge, minCoinChallenge); }
/// <summary> /// Expects a tree with "sqrt" and some other unresolved functions. Returns /// that with all "sqrt" and other replaced /// </summary> /// <param name="tree"></param> /// <returns></returns> internal static Entity Synonymize(Entity tree) { for (int i = 0; i < tree.Children.Count; i++) { tree.Children[i] = Synonymize(tree.Children[i]); } if (SynFunctions.ContainsKey(tree.Name)) { return(SynFunctions[tree.Name](tree.Children)); } else { return(tree); } }
public Script() { // 寄存器初始化 _ret_val = new Op { type = OpType.Null }; funcTable = new FuncTable(); symbolTable = new SymbolTable(); labelTable = new LabelTable(); stringTable = new StringTable(); instrStream = new InstrStream(); stack = new RuntimeStack(); // 初始化运行时堆栈大小 stack.SetStackSize(1024); }
public Entry() : base() { integerReverse = new IntegerReverse(); palindromeNumber = new PalindromeNumber(); romanToInteger = new RomanToInteger(); validParentheses = new ValidParentheses(); findLongestCommonPrefix = new FindLongestCommonPrefix(); mergeTwoSortedArray = new MergeTwoSortedArray(); FuncTable.Add((int)FunctionIndex.IntegerReverse, integerReverse); FuncTable.Add((int)FunctionIndex.PalindromeNumber, palindromeNumber); FuncTable.Add((int)FunctionIndex.RomanToInteger, romanToInteger); FuncTable.Add((int)FunctionIndex.ValidParentheses, validParentheses); FuncTable.Add((int)FunctionIndex.FindLongestCommonPrefix, findLongestCommonPrefix); FuncTable.Add((int)FunctionIndex.MergeTwoSortedArray, mergeTwoSortedArray); }
//TODO TESTS private static void Initialization(ListController <TokenModel> lc) { var type = lc.GetNext(); var name = TryGetNext(lc, "Variable name expected"); if (!Constants.Alphabet.Contains(name.Text[0])) { throw new ParseException("Unexpected token", name); } if (VarTable.Contains(name.Text) || FuncTable.Contains(name.Text)) { throw new ParseException("Member with the same name is already declared", name); } VarTable.Add(name.Text, type.Text); }
public LiteValue Visit(SyntaxCallFunctionExpressionNode Node, LiteEnv Env) { var FuncIndex = LiteValue.Nil; var FuncName = Node.GetFuncIdentNode(); if (FuncName.GetType() == SyntaxNodeType.CallFunctionExpression) { FuncIndex = Visit(FuncName as SyntaxCallFunctionExpressionNode, Env); } else if (FuncName.GetType() == SyntaxNodeType.DotClassExpression) { var DotNode = FuncName as SyntaxDotClassExpressionNode; FuncIndex = Visit(DotNode, Env); if ((DotNode.GetCallIdentNode() as SyntaxIdentifierNode).GetValue() == "New") { return(FuncIndex); } } else if (FuncName.GetType() == SyntaxNodeType.Identifier) { FuncIndex = Env.Get((FuncName as SyntaxIdentifierNode).GetValue()); } if (FuncIndex == LiteValue.Nil || FuncIndex.Type != LiteValueType.Function) { Logger.DError($"unknown function : {FuncName}"); return(LiteValue.Error); } var Func = FuncTable.GetFunc((int)FuncIndex.Numeric); if (Func == null) { Logger.DError($"=> unknown fn name : {FuncIndex.Numeric}"); return(LiteValue.Error); } return(CallFunc(Func, Node.GetArgumentListNode() as SyntaxArgumentListStatementNode, Env)); }
//TODO TESTS private static AssingNode VariableAssign(ListController <TokenModel> lc) { var variableToken = lc.GetNext(); CheckEqual(TryGetNext(lc, "= expected"), "=", "= expected"); var func = TryGetNext(lc, "Expression expected"); var res = new AssingNode(); var variable = VarTable.Get(variableToken.Text); res.Left = variable; if (FuncTable.Contains(func.Text)) { lc.SetBack(); var fn = FuncExecute(lc); res.Right = fn; } else { throw new ParseException("Function expected", func); } return(res); }
static SyntaxAnalyzer() { VarTable = new VarTable(); FuncTable = new FuncTable(); }
public void Register(string Name, LiteLangNativeFunc Func) { Env_.SetSelf(Name, FuncTable.AddFuncEx(new FuncNative(Name, Env_, Func))); }