public AdditionExpressionNode(Parser parser) : base(parser) { leftExpression = Node(parser, 0); action = Token(parser, 1).ToString(); rightExpression = Node(parser, 2); }
public ArrayNode(Parser theParser) : base(theParser) { _array = new List<PSObject>(); lValue = Node(theParser, 0); rValue = Node(theParser, 2); }
private Expression VisitInteger(ASTNode node) { return(Expression.Constant(int.Parse(node.Value))); }
public PluralExpressionCompiler(ASTNode syntaxTree) { _syntaxTree = syntaxTree; }
private static NumberExpression GetNumberExpression(ASTNode prev, Double result) { return(new NumberExpression(prev.Parent, prev.Scope, GetTokenList("number", result.ToString( ), result, TokenType.Number, AdjustRange(prev.Range, result.ToString( ))))); }
void multiplyAssignmentsToSIdByFunction(string id, ASTNode function) { libsbmlPINVOKE.KineticLaw_multiplyAssignmentsToSIdByFunction(swigCPtr, id, ASTNode.getCPtr(function)); }
void divideAssignmentsToSIdByFunction(string id, ASTNode function) { libsbmlPINVOKE.EventAssignment_divideAssignmentsToSIdByFunction(swigCPtr, id, ASTNode.getCPtr(function)); }
public void AddItem(ASTNode item) { Pipeline.Add(item); }
public void Execute(ASTNode program) { TypeSwitch.Do(program, TypeSwitch.Case <AST.VariableDeclareStatement>( varDecl => { var v = Evaluate(varDecl); Console.WriteLine($"var {varDecl.ident.token.lexeme} <- {Stringify(v)}"); }), TypeSwitch.Case <AST.ExpressionStatement>( expr => { var v = Evaluate(expr); if (expr.Expression is AST.Assignment) { var ass = expr.Expression as Assignment; //Console.WriteLine($"{ass.ident.token.lexeme} <- {Stringify(v)}"); } Console.WriteLine(Stringify(v)); }), TypeSwitch.Case <AST.Block>( block => { foreach (var stmt in block.Statements) { Execute(stmt); } }), TypeSwitch.Case <AST.IfStatement>( ifStmt => { var condition = Evaluate(ifStmt.condition); if (Truthify(condition)) { Execute(ifStmt.ifBody); } else if (ifStmt.elseBody != null) { Execute(ifStmt.elseBody); } }), TypeSwitch.Case <AST.WhileStatement>( whileStmt => { while (Truthify(Evaluate(whileStmt.condition))) { Execute(whileStmt.body); if (breakFlag) { break; } } }), TypeSwitch.Case <AST.MatchStatement>( matchStmt => { var value = Evaluate(matchStmt.expression); var matchedCase = matchStmt.matchCases.FirstOrDefault(mc => mc.Type == value.Type); if (matchedCase != null) { Execute(matchedCase.Statement); } else { if (matchStmt.defaultCase != null) { Execute(matchStmt.defaultCase); } } }) ); }
public CloneNode(Token sourceToken, ASTNode cloneExpr) : base(sourceToken) { CloneExpr = cloneExpr; }
int setMath(ASTNode math) { int ret = libsbmlPINVOKE.Trigger_setMath(swigCPtr, ASTNode.getCPtr(math)); return(ret); }
/// <summary> /// Constructs the code from a node. DO NOT CALL THIS /// INTERNALLY, CALL <see cref="ConstructInternal(ASTNode)" />. /// </summary> /// <param name="node"></param> /// <param name="stream"></param> public virtual void Construct(ASTNode node, Stream stream) { this.IndentLevel = 0; using (this.StreamWriter = new StreamWriter(stream, Encoding.GetEncoding(28591), 4096, true)) this.ConstructInternal(node); }
/// <summary> /// Call this on the various nodes so that they are all /// written to the stream. DO NOT CALL /// <see cref="Construct(ASTNode)" /> INTERNALLY. /// </summary> /// <param name="node"></param> protected virtual void ConstructInternal(ASTNode node) { #region Regex Generated Mess if (node is DoStatement) { this.ConstructDoStatement(( DoStatement )node); } else if (node is StatementList) { this.ConstructStatementList(( StatementList )node); } else if (node is AssignmentStatement) { this.ConstructAssignmentStatement(( AssignmentStatement )node); } else if (node is LocalVariableStatement) { this.ConstructLocalVariableStatement(( LocalVariableStatement )node); } else if (node is VariableExpression) { this.ConstructVariableExpression(( VariableExpression )node); } else if (node is BinaryOperatorExpression) { this.ConstructBinaryOperatorExpression(( BinaryOperatorExpression )node); } else if (node is UnaryOperatorExpression) { this.ConstructUnaryOperatorExpression(( UnaryOperatorExpression )node); } else if (node is ForGenericStatement) { this.ConstructForGenericStatement(( ForGenericStatement )node); } else if (node is ForNumericStatement) { this.ConstructForNumericStatement(( ForNumericStatement )node); } else if (node is RepeatStatement) { this.ConstructRepeatStatement(( RepeatStatement )node); } else if (node is WhileStatement) { this.ConstructWhileStatement(( WhileStatement )node); } else if (node is Parsing.Nodes.Indexers.IndexExpression) { this.ConstructIndexExpression((Parsing.Nodes.Indexers.IndexExpression)node); } else if (node is Parsing.Nodes.Indexers.MemberExpression) { this.ConstructMemberExpression((Parsing.Nodes.Indexers.MemberExpression)node); } else if (node is IfClause) { this.ConstructIfClause(( IfClause )node); } else if (node is IfStatement) { this.ConstructIfStatement(( IfStatement )node); } else if (node is AnonymousFunctionExpression) { this.ConstructAnonymousFunctionExpression(( AnonymousFunctionExpression )node); } else if (node is FunctionCallExpression) { this.ConstructFunctionCallExpression(( FunctionCallExpression )node); } else if (node is LocalFunctionStatement) { this.ConstructLocalFunctionStatement(( LocalFunctionStatement )node); } else if (node is NamedFunctionStatement) { this.ConstructNamedFunctionStatement(( NamedFunctionStatement )node); } else if (node is StringFunctionCallExpression) { this.ConstructStringFunctionCallExpression(( StringFunctionCallExpression )node); } else if (node is TableFunctionCallExpression) { this.ConstructTableFunctionCallExpression(( TableFunctionCallExpression )node); } else if (node is BreakStatement) { this.ConstructBreakStatement(( BreakStatement )node); } else if (node is ContinueStatement) { this.ConstructContinueStatement(( ContinueStatement )node); } else if (node is GotoStatement) { this.ConstructGotoStatement(( GotoStatement )node); } else if (node is GotoLabelStatement) { this.ConstructGotoLabelStatement(( GotoLabelStatement )node); } else if (node is ReturnStatement) { this.ConstructReturnStatement(( ReturnStatement )node); } else if (node is BooleanExpression) { this.ConstructBooleanExpression(( BooleanExpression )node); } else if (node is Eof) { this.ConstructEof(( Eof )node); } else if (node is NilExpression) { this.ConstructNilExpression(( NilExpression )node); } else if (node is NumberExpression) { this.ConstructNumberExpression(( NumberExpression )node); } else if (node is ParenthesisExpression) { this.ConstructParenthesisExpression(( ParenthesisExpression )node); } else if (node is StringExpression) { this.ConstructStringExpression(( StringExpression )node); } else if (node is TableConstructorExpression) { this.ConstructTableConstructorExpression(( TableConstructorExpression )node); } else if (node is VarArgExpression) { this.ConstructVarArgExpression(( VarArgExpression )node); } #endregion Regex Generated Mess else { throw new Exception("Unrecognized node: " + node); } }
void replaceSIDWithFunction(string id, ASTNode function) { libsbmlPINVOKE.StoichiometryMath_replaceSIDWithFunction(swigCPtr, id, ASTNode.getCPtr(function)); }
void divideAssignmentsToSIdByFunction(string id, ASTNode function) { libsbmlPINVOKE.InitialAssignment_divideAssignmentsToSIdByFunction(swigCPtr, id, ASTNode.getCPtr(function)); if (libsbmlPINVOKE.SWIGPendingException.Pending) { throw libsbmlPINVOKE.SWIGPendingException.Retrieve(); } }
public void Add(ASTNode child) { mChildren.Add(child); }
public void assertType(ASTNode n, string s, Type t, Value v) { assertType(n.mToken.Key, s, t, v); }
private StackValue Evaluate(ASTNode Expression) { #region operation int IntegerOperation(StackValue operand1, StackValue operand2, TokenType op) { int i1 = (int)operand1.Value; int i2 = (int)operand2.Value; switch (op) { case TokenType.PLUS: return(i1 + i2); case TokenType.MINUS: return(i1 - i2); case TokenType.MULTIPLY: return(i1 * i2); case TokenType.DIVIDE: return(i1 / i2); case TokenType.MODULO: return(i1 % i2); } return(0); } float FloatOperation(StackValue operand1, StackValue operand2, TokenType op) { float f1 = operand1.Value is float?(float)operand1.Value : (int)operand1.Value; float f2 = operand2.Value is float?(float)operand2.Value : (int)operand2.Value; switch (op) { case TokenType.PLUS: return(f1 + f2); case TokenType.MINUS: return(f1 - f2); case TokenType.MULTIPLY: return(f1 * f2); case TokenType.DIVIDE: return(f1 / f2); case TokenType.EXPONENT: return((float)Math.Pow(f1, f2)); } return(0); } string StringOperation(StackValue operand1, StackValue operand2, TokenType op) { string s1 = operand1.Value.ToString(); string s2 = operand2.Value.ToString(); if (op == TokenType.PLUS) { return(s1 + s2); } return(""); } bool BoolOperation(StackValue operand1, StackValue operand2, TokenType op) { bool b1 = (bool)operand1.Value; bool b2 = op == TokenType.NOT ? true : (bool)operand2.Value; switch (op) { case TokenType.AND: return(b1 && b2); case TokenType.OR: return(b1 || b2); case TokenType.XOR: return(b1 ^ b2); case TokenType.NOT: return(!b1); } return(false); } bool IntComparison(StackValue operand1, StackValue operand2, TokenType op) { int i1 = (int)operand1.Value; int i2 = (int)operand2.Value; switch (op) { case TokenType.EQUAL: return(i1 == i2); case TokenType.NOTEQUAL: return(i1 != i2); case TokenType.LARGER: return(i1 > i2); case TokenType.LARGEREQUAL: return(i1 >= i2); case TokenType.LESSER: return(i1 < i2); case TokenType.LESSEREQUAL: return(i1 <= i2); } return(false); } bool FloatComparison(StackValue operand1, StackValue operand2, TokenType op) { float f1 = (float)operand1.Value; float f2 = (float)operand2.Value; switch (op) { case TokenType.EQUAL: return(f1 == f2); case TokenType.NOTEQUAL: return(f1 != f2); case TokenType.LARGER: return(f1 > f2); case TokenType.LARGEREQUAL: return(f1 >= f2); case TokenType.LESSER: return(f1 < f2); case TokenType.LESSEREQUAL: return(f1 <= f2); } return(false); } bool CharComparison(StackValue operand1, StackValue operand2, TokenType op) { char c1 = (char)operand1.Value; char c2 = (char)operand2.Value; switch (op) { case TokenType.EQUAL: return(c1 == c2); case TokenType.NOTEQUAL: return(c1 != c2); case TokenType.LARGER: return(c1 > c2); case TokenType.LARGEREQUAL: return(c1 >= c2); case TokenType.LESSER: return(c1 < c2); case TokenType.LESSEREQUAL: return(c1 <= c2); } return(false); } bool StringComparison(StackValue operand1, StackValue operand2, TokenType op) { string s1 = (string)operand1.Value; string s2 = (string)operand2.Value; var comparison = s1.CompareTo(s2); switch (op) { case TokenType.EQUAL: return(s1.Equals(s2)); case TokenType.NOTEQUAL: return(!s1.Equals(s2)); case TokenType.LARGER: return(comparison > 0); case TokenType.LARGEREQUAL: return(comparison >= 0); case TokenType.LESSER: return(comparison < 0); case TokenType.LESSEREQUAL: return(comparison <= 0); } return(false); } bool BoolComparison(StackValue operand1, StackValue operand2, TokenType op) { bool b1 = (bool)operand1.Value; bool b2 = (bool)operand2.Value; switch (op) { case TokenType.EQUAL: return(b1 == b2); case TokenType.NOTEQUAL: return(b1 != b2); } return(false); } bool Comparison(StackValue operand1, StackValue operand2, TokenType op) { bool result = true; switch (operand1.Type) { case ValType.Integer: int i1 = (int)operand1.Value; if (operand2.Type == ValType.Float) { //do int comparison int i2 = (int)(float)operand2.Value; switch (op) { case TokenType.EQUAL: result = i1 == i2; break; case TokenType.NOTEQUAL: result = i1 != i2; break; case TokenType.LARGER: result = i1 > i2; break; case TokenType.LARGEREQUAL: result = i1 >= i2; break; case TokenType.LESSER: result = i1 < i2; break; case TokenType.LESSEREQUAL: result = i1 <= i2; break; } } else if (operand2.Type == ValType.Char) { //do int comparison int i2 = (int)(char)operand2.Value; switch (op) { case TokenType.EQUAL: result = i1 == i2; break; case TokenType.NOTEQUAL: result = i1 != i2; break; case TokenType.LARGER: result = i1 > i2; break; case TokenType.LARGEREQUAL: result = i1 >= i2; break; case TokenType.LESSER: result = i1 < i2; break; case TokenType.LESSEREQUAL: result = i1 <= i2; break; } } else { BinaryRuntimeError(operand1, operand2, op); } break; case ValType.Float: float f1 = (float)operand1.Value; if (operand2.Type == ValType.Integer) { //do flt math float f2 = (float)(int)operand2.Value; switch (op) { case TokenType.EQUAL: result = f1 == f2; break; case TokenType.NOTEQUAL: result = f1 != f2; break; case TokenType.LARGER: result = f1 > f2; break; case TokenType.LARGEREQUAL: result = f1 >= f2; break; case TokenType.LESSER: result = f1 < f2; break; case TokenType.LESSEREQUAL: result = f1 <= f2; break; } } else { BinaryRuntimeError(operand1, operand2, op); } break; case ValType.Char: int c1 = (int)(char)operand1.Value; if (operand2.Type == ValType.Integer) { //do int comparison int c2 = (int)(char)operand2.Value; switch (op) { case TokenType.EQUAL: result = c1 == c2; break; case TokenType.NOTEQUAL: result = c1 != c2; break; case TokenType.LARGER: result = c1 > c2; break; case TokenType.LARGEREQUAL: result = c1 >= c2; break; case TokenType.LESSER: result = c1 < c2; break; case TokenType.LESSEREQUAL: result = c1 <= c2; break; } } else { BinaryRuntimeError(operand1, operand2, op); } break; case ValType.Bool: //cause there is no boolean comparison operator with mixed type BinaryRuntimeError(operand1, operand2, op); break; case ValType.String: //cause there is no string comparison operator with mixed type BinaryRuntimeError(operand1, operand2, op); break; case ValType.Null: BinaryRuntimeError(operand1, operand2, op); break; } return(result); } bool TypeTesting(StackValue operand1, StackValue operand2, TokenType op) { ValType type = operand2.CastTo <ValType>(); switch (op) { case TokenType.EQUAL: case TokenType.NOTEQUAL: if (operand1.Type != ValType.Type) { return(false); } else { var type2 = operand1.CastTo <ValType>(); return(op == TokenType.EQUAL ? type == type2 : !(type == type2)); } case TokenType.IS: // <value> is <Type> return(operand1.Type == type); } return(false); } #endregion #region evaluate expression void EvaluateBinaryOperation(StackValue operand1, StackValue operand2, TokenType op) { var v1 = ResolveStackValue(operand1); var v2 = ResolveStackValue(operand2); if (v1.Type == v2.Type) { switch (v1.Type) { case ValType.Integer: switch (op) { case TokenType.PLUS: case TokenType.MINUS: case TokenType.MULTIPLY: case TokenType.DIVIDE: case TokenType.MODULO: Push(IntegerOperation(v1, v2, op)); break; case TokenType.EXPONENT: Push(FloatOperation(v1, v2, TokenType.EXPONENT)); break; case TokenType.EQUAL: case TokenType.NOTEQUAL: case TokenType.LARGER: case TokenType.LARGEREQUAL: case TokenType.LESSER: case TokenType.LESSEREQUAL: Push(IntComparison(v1, v2, op)); break; default: BinaryRuntimeError(v1, v2, op); break; } break; case ValType.Float: switch (op) { case TokenType.PLUS: case TokenType.MINUS: case TokenType.MULTIPLY: case TokenType.DIVIDE: case TokenType.EXPONENT: Push(FloatOperation(v1, v2, op)); break; case TokenType.EQUAL: case TokenType.NOTEQUAL: case TokenType.LARGER: case TokenType.LARGEREQUAL: case TokenType.LESSER: case TokenType.LESSEREQUAL: Push(FloatComparison(v1, v2, op)); break; default: BinaryRuntimeError(v1, v2, op); break; } break; case ValType.Bool: switch (op) { case TokenType.AND: case TokenType.OR: case TokenType.XOR: Push(BoolOperation(v1, v2, op)); break; case TokenType.EQUAL: case TokenType.NOTEQUAL: Push(BoolComparison(v1, v2, op)); break; default: BinaryRuntimeError(v1, v2, op); break; } break; case ValType.Char: //concat char switch (op) { case TokenType.PLUS: Push(StringOperation(v1, v2, TokenType.PLUS)); break; case TokenType.EQUAL: case TokenType.NOTEQUAL: case TokenType.LARGER: case TokenType.LARGEREQUAL: case TokenType.LESSER: case TokenType.LESSEREQUAL: Push(CharComparison(v1, v2, op)); break; default: BinaryRuntimeError(v1, v2, op); break; } break; case ValType.String: //concat string switch (op) { case TokenType.PLUS: Push(StringOperation(v1, v2, TokenType.PLUS)); break; case TokenType.EQUAL: case TokenType.NOTEQUAL: case TokenType.LARGER: case TokenType.LARGEREQUAL: case TokenType.LESSER: case TokenType.LESSEREQUAL: Push(StringComparison(v1, v2, op)); break; default: BinaryRuntimeError(v1, v2, op); break; } break; case ValType.Null: BinaryRuntimeError(v1, v2, op); break; case ValType.Type: switch (op) { case TokenType.EQUAL: case TokenType.NOTEQUAL: Push(TypeTesting(v1, v2, op)); break; default: BinaryRuntimeError(v1, v2, op); break; } break; default: switch (op) { case TokenType.IS: Push(TypeTesting(v1, v2, op)); break; default: BinaryRuntimeError(v1, v2, op); break; } break; } } else { switch (v1.Type) { case ValType.Integer: if (v2.Type == ValType.Float) { //do flt math switch (op) { case TokenType.PLUS: case TokenType.MINUS: case TokenType.MULTIPLY: case TokenType.DIVIDE: case TokenType.EXPONENT: Push(FloatOperation(v1, v2, op)); break; case TokenType.EQUAL: case TokenType.NOTEQUAL: case TokenType.LARGER: case TokenType.LARGEREQUAL: case TokenType.LESSER: case TokenType.LESSEREQUAL: Push(Comparison(v1, v2, op)); break; } } else if (v2.Type == ValType.Char) { //do int math switch (op) { case TokenType.PLUS: case TokenType.MINUS: case TokenType.MULTIPLY: case TokenType.DIVIDE: Push(IntegerOperation(v1, v2, op)); break; case TokenType.EXPONENT: Push(FloatOperation(v1, v2, TokenType.EXPONENT)); break; case TokenType.EQUAL: case TokenType.NOTEQUAL: case TokenType.LARGER: case TokenType.LARGEREQUAL: case TokenType.LESSER: case TokenType.LESSEREQUAL: Push(Comparison(v1, v2, op)); break; } } else if (op == TokenType.IS) { Push(TypeTesting(v1, v2, op)); } else { BinaryRuntimeError(v1, v2, op); } break; case ValType.Float: if (v2.Type == ValType.Integer) { //do flt math switch (op) { case TokenType.PLUS: case TokenType.MINUS: case TokenType.MULTIPLY: case TokenType.DIVIDE: case TokenType.EXPONENT: Push(FloatOperation(v1, v2, op)); break; case TokenType.EQUAL: case TokenType.NOTEQUAL: case TokenType.LARGER: case TokenType.LARGEREQUAL: case TokenType.LESSER: case TokenType.LESSEREQUAL: Push(Comparison(v1, v2, op)); break; } } else if (op == TokenType.IS) { Push(TypeTesting(v1, v2, op)); } else { BinaryRuntimeError(v1, v2, op); } break; case ValType.Char: switch (op) { case TokenType.EQUAL: case TokenType.NOTEQUAL: case TokenType.LARGER: case TokenType.LARGEREQUAL: case TokenType.LESSER: case TokenType.LESSEREQUAL: Push(Comparison(v1, v2, op)); break; case TokenType.IS: Push(TypeTesting(v1, v2, op)); break; default: BinaryRuntimeError(v1, v2, op); break; } break; case ValType.Bool: if (op == TokenType.IS) { Push(TypeTesting(v1, v2, op)); } else { BinaryRuntimeError(v1, v2, op); } break; case ValType.String: //concat string if (op == TokenType.PLUS) { Push(StringOperation(v1, v2, TokenType.PLUS)); } else if (op == TokenType.IS) { Push(TypeTesting(v1, v2, op)); } else if (op == TokenType.IS) { Push(TypeTesting(v1, v2, op)); } else { BinaryRuntimeError(v1, v2, op); } break; default: switch (op) { case TokenType.IS: Push(TypeTesting(v1, v2, op)); break; default: BinaryRuntimeError(v1, v2, op); break; } break; } } } void EvaluateUnaryOperation(StackValue operand, TokenType op) { var v = ResolveStackValue(operand); switch (op) { case TokenType.MINUS: if (v.Type == ValType.Integer) { int i = v.CastTo <int>(); Push(-i); } else if (v.Type == ValType.Float) { float f = v.CastTo <float>(); Push(-f); } else { UnaryRuntimeError(operand, op); } break; case TokenType.NOT: if (v.Type == ValType.Bool) { bool b = v.CastTo <bool>(); Push(!b); } break; case TokenType.TYPEOF: Push(v.Type); break; } } #endregion #region visit the astnode Expression.Accept(this); #endregion #region process operand if (EvaluationStack.Count == 0) { return(StackValue.Null); } ReverseStack(); while (EvaluationStack.Count > 1) { StackValue operand1 = EvaluationStack.Pop(); StackValue operand2 = EvaluationStack.Pop(); TokenType op; if (operand2.Type == ValType.Operator) { //unary opearation op = (TokenType)operand2.Value; EvaluateUnaryOperation(operand1, op); } else { var tots = EvaluationStack.Pop(); op = (TokenType)tots.Value; if (op == TokenType.ASSIGN) { //do assignment //todo check type var varname = (string)operand2.Value; if (!CurrentScope.Contain(varname)) { RuntimeError($"{varname} is not defined"); } CurrentScope.Assign(varname, ResolveStackValue(operand1).Value); Push(ResolveStackValue(operand2)); } else if (op == TokenType.VAR) { //declare a variable in the environment var varname = operand2.CastTo <string>(); if (CurrentScope.Contain(varname)) { RuntimeError($"{varname} already defined"); } CurrentScope.Define(varname, ResolveStackValue(operand1).Value); Push(ResolveStackValue(operand2)); } else { EvaluateBinaryOperation(operand1, operand2, op); } } } return(ResolveStackValue(EvaluationStack.Pop())); #endregion }
int setMath(ASTNode math) { int ret = libsbmlPINVOKE.Constraint_setMath(swigCPtr, ASTNode.getCPtr(math)); return(ret); }
void multiplyAssignmentsToSIdByFunction(string id, ASTNode function) { libsbmlPINVOKE.EventAssignment_multiplyAssignmentsToSIdByFunction(swigCPtr, id, ASTNode.getCPtr(function)); if (libsbmlPINVOKE.SWIGPendingException.Pending) { throw libsbmlPINVOKE.SWIGPendingException.Retrieve(); } }
public ReturnAstNode(ASTNode returnexpr, IToken token) : base(token) { ReturnExpression = returnexpr; Children.Add(returnexpr); }
int setMath(ASTNode math) { int ret = libsbmlPINVOKE.StoichiometryMath_setMath(swigCPtr, ASTNode.getCPtr(math)); return(ret); }
void replaceSIDWithFunction(string id, ASTNode function) { libsbmlPINVOKE.KineticLaw_replaceSIDWithFunction(swigCPtr, id, ASTNode.getCPtr(function)); }
void AddNew(ASTNode node, TreeNodeCollection parent) { switch (node.type) { case NodeType.USING: var unode = node as UsingNode; parent.Add("导入包 " + unode.path); break; case NodeType.CLASS: var cnode = node as ClassNode; var temp = parent.Add((cnode.isstatic ? "静态 " : "") + (cnode.AUTHORITY == TokenType.PUBLIC_TOKEN?"公开":"私有") + " 类 " + cnode.className); var body = cnode.body as BlockNode; foreach (var item in body.targets) { AddNew(item, temp.Nodes); } break; case NodeType.FUNC: var fnode = node as FuncNode; temp = parent.Add((fnode.isstatic ? "静态 " : "") + (fnode.authority == TokenType.PUBLIC_TOKEN ? "公开" : "私有") + " 函数 " + fnode.funcName); var temp1 = temp.Nodes.Add("参数"); var args = fnode.args as BlockNode; foreach (var item in args.targets) { AddNew(item, temp1.Nodes); } temp1 = temp.Nodes.Add("逻辑块"); body = fnode.body as BlockNode; foreach (var item in body.targets) { AddNew(item, temp1.Nodes); } break; case NodeType.CALCULATE: var canode = node as CalculateNode; if (canode.symbol == "call") { var callnode1 = canode.right as CallNode; temp = parent.Add("调用方法 " + callnode1.funcName); temp1 = temp.Nodes.Add("参数"); args = callnode1.args as BlockNode; foreach (var item in args.targets) { AddNew(item, temp1.Nodes); } AddNew(canode.left, temp.Nodes); } else { temp = parent.Add("运算 " + canode.symbol); AddNew(canode.left, temp.Nodes); AddNew(canode.right, temp.Nodes); } break; case NodeType.VAR: var vnode = node as VarNode; if (vnode != null) { temp = parent.Add("变量 " + vnode.varType + " " + vnode.varName); if (vnode.value != null) { AddNew(vnode.value, temp.Nodes); } } else { var cvnode = node as ClassVarNode; temp = parent.Add((cvnode.isstatic?"静态 ":"") + (cvnode.authority == TokenType.PUBLIC_TOKEN?"公开 ":"私有 ") + "成员变量 " + cvnode.varType + " " + cvnode.varName); if (cvnode.body != null) { AddNew(cvnode.body, temp.Nodes); } } break; case NodeType.BLOCK: var bnode = node as BlockNode; temp = parent.Add("root"); foreach (var item in bnode.targets) { AddNew(item, temp.Nodes); } break; case NodeType.CALL: var callnode = node as CallNode; temp = parent.Add("调用方法 " + callnode.funcName); temp1 = temp.Nodes.Add("参数"); args = callnode.args as BlockNode; foreach (var item in args.targets) { AddNew(item, temp1.Nodes); } break; case NodeType.IF_ELSE: break; case NodeType.FOR: break; case NodeType.WHILE: break; case NodeType.ENUM: break; case NodeType.VALUE: var vanode = node as ValueNode; temp = parent.Add(/*GetVType(vanode.valueType)+ " " +*/ vanode.value); break; } }
int setMath(ASTNode math) { int ret = libsbmlPINVOKE.KineticLaw_setMath(swigCPtr, ASTNode.getCPtr(math)); return(ret); }
public void prepend(ASTNode item) { libsbmlPINVOKE.ASTNodeList_prepend(swigCPtr, ASTNode.getCPtr(item)); }
private static StringExpression GetStringExpression(ASTNode prev, String result) { return(new StringExpression(prev.Parent, prev.Scope, GetTokenList("string", $"'{result.Replace ( "'", "\\'" )}'", result, TokenType.String, AdjustRange(prev.Range, result + "..")) )); }
public MemberExpression(ASTNode parent, Scope scope, IList <LToken> tokens) : base(parent, scope, tokens) { }
private Expression VisitVariable(ASTNode node) { return(_param); }
static double evaluateASTNode(ASTNode node) { double ret = libsbmlPINVOKE.SBMLTransforms_evaluateASTNode__SWIG_1(ASTNode.getCPtr(node)); return(ret); }
private ASTNode AssembleRecursive(IEnumerator<KeyValuePair<string, Tokens>> tokens) { if (!tokens.MoveNext()) throw new ParseError("Out of tokens"); KeyValuePair<string, Tokens> token = tokens.Current; ASTNode n = new ASTNode(token); ASTNode left, right, child, cond, actn; switch(token.Value) { case Tokens.TOK_COMMENT: break; case Tokens.TOK_KEYWORD: if (token.Key.Equals("ON")) { cond = AssembleRecursive(tokens); if (cond.mToken.Value == Tokens.TOK_KEYWORD) throw new ParseError("ON cond was bad: " + cond.ToString()); n.Add(cond); actn = AssembleRecursive(tokens); if (actn.mToken.Value != Tokens.TOK_KEYWORD || !(actn.mToken.Key.Equals("DO") || actn.mToken.Key.Equals("HIBERNATE"))) throw new ParseError("ON DO was bad: " + actn.ToString()); n.Add(actn); break; } if (token.Key.Equals("IF")) { cond = AssembleRecursive(tokens); if (cond.mToken.Value == Tokens.TOK_KEYWORD) throw new ParseError("IF cond was bad: " + cond.ToString()); n.Add(cond); actn = AssembleRecursive(tokens); if (actn.mToken.Value != Tokens.TOK_KEYWORD || !actn.mToken.Key.Equals("THEN")) throw new ParseError("IF THEN was bad: " + actn.ToString()); n.Add(actn); break; } if (token.Key.Equals("DO")) { actn = AssembleRecursive(tokens); if (actn.mToken.Value != Tokens.TOK_SEMI && actn.mToken.Value != Tokens.TOK_AT) throw new ParseError("DO actn was bad: " + actn.ToString()); n.Add(actn); break; } if (token.Key.Equals("THEN")) { actn = AssembleRecursive(tokens); if (actn.mToken.Value != Tokens.TOK_SEMI && actn.mToken.Value != Tokens.TOK_AT) throw new ParseError("THEN actn was bad: " + actn.ToString()); n.Add(actn); break; } if (token.Key.Equals("HIBERNATE")) { cond = AssembleRecursive(tokens); if (cond.mToken.Value == Tokens.TOK_KEYWORD || cond.mToken.Value == Tokens.TOK_AT || cond.mToken.Value == Tokens.TOK_COMMA || cond.mToken.Value == Tokens.TOK_SEMI) throw new ParseError("HIBERNATE wake-up condition was bad: " + cond.ToString()); n.Add(cond); break; } throw new ParseError(token.Key); // can't happen case Tokens.TOK_LOG_OP: case Tokens.TOK_ARITH_OP: case Tokens.TOK_COMP_OP: left = AssembleRecursive(tokens); if (left.mToken.Value == Tokens.TOK_KEYWORD || left.mToken.Value == Tokens.TOK_AT || left.mToken.Value == Tokens.TOK_COMMA || left.mToken.Value == Tokens.TOK_SEMI) throw new ParseError(token.Key + " left expr was bad: " + left.ToString()); n.Add(left); right = AssembleRecursive(tokens); if (right.mToken.Value == Tokens.TOK_KEYWORD || right.mToken.Value == Tokens.TOK_AT || right.mToken.Value == Tokens.TOK_COMMA || right.mToken.Value == Tokens.TOK_SEMI) throw new ParseError(token.Key + " right expr was bad: " + right.ToString()); n.Add(right); break; case Tokens.TOK_UN_OP: child = AssembleRecursive(tokens); if (child.mToken.Value == Tokens.TOK_KEYWORD || child.mToken.Value == Tokens.TOK_AT || child.mToken.Value == Tokens.TOK_COMMA || child.mToken.Value == Tokens.TOK_SEMI) throw new ParseError(token.Key + " child expr was bad: " + child.ToString()); n.Add(child); break; case Tokens.TOK_IDENT: case Tokens.TOK_LITERAL: break; case Tokens.TOK_AT: left = AssembleRecursive(tokens); if (left.mToken.Value != Tokens.TOK_IDENT) throw new ParseError(token.Key + " left expr was bad: " + left.ToString()); n.Add(left); right = AssembleRecursive(tokens); if (right.mToken.Value == Tokens.TOK_KEYWORD || right.mToken.Value == Tokens.TOK_AT || right.mToken.Value == Tokens.TOK_SEMI) throw new ParseError(token.Key + " right expr was bad: " + right.ToString()); n.Add(right); break; case Tokens.TOK_COMMA: left = AssembleRecursive(tokens); if (left.mToken.Value == Tokens.TOK_KEYWORD || left.mToken.Value == Tokens.TOK_AT || left.mToken.Value == Tokens.TOK_SEMI) throw new ParseError(token.Key + " left expr was bad: " + left.ToString()); n.Add(left); right = AssembleRecursive(tokens); if (right.mToken.Value == Tokens.TOK_KEYWORD || right.mToken.Value == Tokens.TOK_AT || right.mToken.Value == Tokens.TOK_SEMI || right.mToken.Value == Tokens.TOK_COMMA) throw new ParseError(token.Key + " right expr was bad: " + right.ToString()); n.Add(right); break; case Tokens.TOK_SEMI: left = AssembleRecursive(tokens); if (left.mToken.Value != Tokens.TOK_IDENT && left.mToken.Value != Tokens.TOK_AT && left.mToken.Value != Tokens.TOK_SEMI) throw new ParseError("; left expr was bad: " + left.ToString()); n.Add(left); right = AssembleRecursive(tokens); if (right.mToken.Value != Tokens.TOK_IDENT && right.mToken.Value != Tokens.TOK_AT) throw new ParseError("; right expr was bad: " + right.ToString()); n.Add(right); break; default: throw new ParseError(token.Value.ToString() + ": " + token.Key); // can't happen } return n; }
static void replaceFD(ASTNode math, FunctionDefinition fd) { libsbmlPINVOKE.SBMLTransforms_replaceFD__SWIG_1(ASTNode.getCPtr(math), FunctionDefinition.getCPtr(fd)); }
public Instruction(string text) { mText = text; List<KeyValuePair<string, Tokens>> tokens = Tokenise(text); if (tokens[0].Value == Tokens.TOK_COMMENT) mImemWords = 0; else mImemWords = tokens.Count; //Logging.Log(string.Format("imemWords = {0:D}", mImemWords)); requiresLevelTrigger = tokens.Any(kvp => kvp.Value == Tokens.TOK_KEYWORD && kvp.Key == "IF"); requiresLogicOps = tokens.Any(kvp => kvp.Value == Tokens.TOK_LOG_OP); requiresArithOps = tokens.Any(kvp => kvp.Value == Tokens.TOK_ARITH_OP); mAST = Assemble(tokens); //Logging.Log(mAST.ToString()); mText = ToString(); }
static void replaceFD(ASTNode math, ListOfFunctionDefinitions lofd, IdList idsToExclude) { libsbmlPINVOKE.SBMLTransforms_replaceFD__SWIG_2(ASTNode.getCPtr(math), ListOfFunctionDefinitions.getCPtr(lofd), IdList.getCPtr(idsToExclude)); }
public Value evalRecursive(ASTNode n, Processor p) { Value left, right; switch(n.mToken.Value) { case Tokens.TOK_ARITH_OP: // + - * / left = evalRecursive(n.mChildren[0], p); right = evalRecursive(n.mChildren[1], p); if ((left.typ == Type.ANGLE) && (right.typ == Type.ANGLE)) { if (n.mToken.Key.Equals("+")) return new Value((left.d + right.d) % 360.0, true); else if (n.mToken.Key.Equals("-")) return new Value((left.d + 360.0 - right.d) % 360.0, true); else if (n.mToken.Key.Equals("*")) throw new EvalError("'*' not valid on angles"); else if (n.mToken.Key.Equals("/")) throw new EvalError("'/' not valid on angles"); else // can't happen throw new EvalError(n.mToken.Key); } else { if (left.typ == Type.ANGLE) left = new Value(left.d); else if (right.typ == Type.ANGLE) right = new Value(right.d); assertType(n, "left", Type.DOUBLE, left); assertType(n, "right", Type.DOUBLE, right); if (n.mToken.Key.Equals("+")) return new Value(left.d + right.d); else if (n.mToken.Key.Equals("-")) return new Value(left.d - right.d); else if (n.mToken.Key.Equals("*")) return new Value(left.d * right.d); else if (n.mToken.Key.Equals("/")) return new Value(left.d / right.d); else // can't happen throw new EvalError(n.mToken.Key); } case Tokens.TOK_AT: left = evalRecursive(n.mChildren[0], p); right = evalRecursive(n.mChildren[1], p); assertType(n, "left", Type.NAME, left); return exec(left.n, right, p); case Tokens.TOK_COMMA: left = evalRecursive(n.mChildren[0], p); right = evalRecursive(n.mChildren[1], p); return new Value(left, right); case Tokens.TOK_COMP_OP: // < > left = evalRecursive(n.mChildren[0], p); right = evalRecursive(n.mChildren[1], p); if (left.typ == Type.ANGLE && right.typ == Type.ANGLE) { double diff = left.d - right.d; if (diff > 180.0) diff -= 360.0; else if (diff < -180.0) diff += 360.0; if (n.mToken.Key.Equals("<")) return new Value(diff < 0); else if (n.mToken.Key.Equals(">")) return new Value(diff > 0); else // can't happen throw new EvalError(n.mToken.Key); } else { if (left.typ == Type.ANGLE) left = new Value(left.d); else if (right.typ == Type.ANGLE) right = new Value(right.d); assertType(n, "left", Type.DOUBLE, left); assertType(n, "right", Type.DOUBLE, right); if (n.mToken.Key.Equals("<")) return new Value(left.d < right.d); else if (n.mToken.Key.Equals(">")) return new Value(left.d > right.d); else // can't happen throw new EvalError(n.mToken.Key); } case Tokens.TOK_IDENT: if (n.mToken.Key.Equals("true")) return new Value(true); if (n.mToken.Key.Equals("false")) return new Value(false); if (n.mToken.Key.Equals("error")) { bool b = p.error; p.error = false; return new Value(b); } if (p.inputValues.ContainsKey(n.mToken.Key)) { return new Value(p.inputValues[n.mToken.Key]); } return new Value(n.mToken.Key); case Tokens.TOK_KEYWORD: // ON DO IF THEN if (n.mToken.Key.Equals("ON")) { Value cond = evalRecursive(n.mChildren[0], p); assertType(n, "cond", Type.BOOLEAN, cond); if (mGlitched) cond = new Value(!cond.b); if (cond.b && !lastValue) { Logging.Log("edge fired! " + mText); if (TimeWarp.CurrentRateIndex > 0) TimeWarp.SetRate(0, true); // force 1x speed try { evalRecursive(n.mChildren[1], p); } catch(Exception ex) { Logging.Message(ex.ToString()); } } lastValue = cond.b; return new Value(); } if (n.mToken.Key.Equals("IF")) { Value cond = evalRecursive(n.mChildren[0], p); assertType(n, "cond", Type.BOOLEAN, cond); if (mGlitched) cond = new Value(!cond.b); if (cond.b) { evalRecursive(n.mChildren[1], p); } return new Value(); } if (n.mToken.Key.Equals("DO")) return evalRecursive(n.mChildren[0], p); if (n.mToken.Key.Equals("THEN")) return evalRecursive(n.mChildren[0], p); if (n.mToken.Key.Equals("HIBERNATE")) { p.hibernate(this); return new Value(); } // can't happen throw new EvalError(n.mToken.Key); case Tokens.TOK_LITERAL: try { if (n.mToken.Key.EndsWith("~")) return new Value(Double.Parse(n.mToken.Key.TrimEnd('~')), true); else return new Value(Double.Parse(n.mToken.Key)); } catch (Exception) // can't happen? { throw new EvalError(n.mToken.Key); } case Tokens.TOK_LOG_OP: // AND OR left = evalRecursive(n.mChildren[0], p); assertType(n, "left", Type.BOOLEAN, left); if (n.mToken.Key.Equals("AND") && !left.b) return new Value(false); else if (n.mToken.Key.Equals("OR") && left.b) return new Value(true); right = evalRecursive(n.mChildren[1], p); assertType(n, "right", Type.BOOLEAN, right); if (n.mToken.Key.Equals("AND")) return new Value(left.b && right.b); else if (n.mToken.Key.Equals("OR")) return new Value(left.b || right.b); else // can't happen throw new EvalError(n.mToken.Key); case Tokens.TOK_SEMI: evalRecursive(n.mChildren[0], p); evalRecursive(n.mChildren[1], p); return new Value(); case Tokens.TOK_UN_OP: // ! if (!n.mToken.Key.Equals("!")) // can't happen throw new EvalError(n.mToken.Key); left = evalRecursive(n.mChildren[0], p); assertType(n, "child", Type.BOOLEAN, left); return new Value(!left.b); case Tokens.TOK_COMMENT: return new Value(); default: // can't happen throw new EvalError(n.mToken.Value.ToString()); } }
static void replaceFD(ASTNode math, ListOfFunctionDefinitions lofd) { libsbmlPINVOKE.SBMLTransforms_replaceFD__SWIG_3(ASTNode.getCPtr(math), ListOfFunctionDefinitions.getCPtr(lofd)); }
public AssignmentNode(Parser theParser) : base(theParser) { lValue = Node(theParser, 0); rValue = Node(theParser, 2); }
int setMath(ASTNode math) { int ret = libsbmlPINVOKE.EventAssignment_setMath(swigCPtr, ASTNode.getCPtr(math)); return(ret); }
public RangeNode(Parser theParser) : base(theParser) { lValue = Node(theParser, 0); rValue = Node(theParser, 2); }
void replaceSIDWithFunction(string id, ASTNode function) { libsbmlPINVOKE.EventAssignment_replaceSIDWithFunction(swigCPtr, id, ASTNode.getCPtr(function)); }
public void Insert(int index, ASTNode item) { Pipeline.Insert(0, item); }
public ValueOfNode(Parser parser) : base(parser) { nodeValue = Node(parser, 1); }