public ForBlock(Expression initialValue, List<Instruction> buffer) : base(buffer) { sForCount++; iterator = "_iterator_"+ sForCount; // won't collide with names that are parse-able buffer.Add(new SetInstruction(iterator, initialValue)); branchIndex = buffer.Count; branch = new BranchInstruction( new BinaryOperationExpression( BinaryOperation.GreaterThan, new VariableExpression(iterator), new LiteralExpression(0)), branchIndex+1, -1 ); buffer.Add(branch); }
bool ParseTokenSlice(List<Token> tokens, int start, int len, out Expression result) { result = null; if (len == 0) { return false; } if (len == 1) { switch(tokens[start].type) { case TokenType.SubExpression: return ParseSubExpression(tokens[start].expr, out result); case TokenType.Element: return ParseElement(tokens[start].expr, out result); default: return false; } } int opIndex; if (FindHighestPriorityOperator(tokens, start, len, out opIndex)) { if (opIndex == start) { Expression right; if (!ParseTokenSlice(tokens, start+1, len-1, out right)) { return false; } result = new UnaryOperationExpression(mUnaryOpTable[mOps[tokens[opIndex].opId]], right); return true; } else { Expression left, right; if (!ParseTokenSlice(tokens, start, opIndex-start, out left)) { return false; } if (!ParseTokenSlice(tokens, opIndex+1, start + len - opIndex - 1, out right)) { return false; } result = new BinaryOperationExpression(mBinaryOpTable[mOps[tokens[opIndex].opId]], left, right); return true; } } else { return false; } }
//--------------------------------------------------------------------- // PARSING METHODS //--------------------------------------------------------------------- bool ParseSubExpression(string expr, out Expression result) { List<Token> tokens; if (!Tokenize(expr, out tokens)) { result = null; return false; } return ParseTokenSlice(tokens, 0, tokens.Count, out result); }
bool ParseElement(string elem, out Expression result) { result = null; elem = elem.Trim(); if (elem.Length == 0) { return false; } if (elem[0] == '(') { elem = elem.Substring(1, elem.Length-2).Trim(); } int val; if (int.TryParse(elem, out val)) { result = new LiteralExpression(val); } else { // verify variable name for(int i=0; i<elem.Length; ++i) { if (!char.IsUpper(elem[i]) || elem[i] == '_' || char.IsNumber(elem[i])) { return false; } } result = new VariableExpression(elem); } return true; }
public BinaryOperationExpression(BinaryOperation op, Expression left, Expression right) { mOp = op; mLeft = left; mRight = right; }
public UnaryOperationExpression(UnaryOperation op, Expression exp) { mOp = op; mExpr = exp; }
public BranchInstruction(Expression condition, int jumpTrue, int jumpFalse) { this.condition = condition; this.indexTrue = jumpTrue; this.indexFalse = jumpFalse; }
public ActionInstruction(Action<int> action, Expression exp) { mAction = action; mExpr = exp; }
public SetInstruction(string name, Expression exp) { mName = name; mExpr = exp; }
public WhileBlock(Expression condition, List<Instruction> buffer) : base(buffer) { branchIndex = buffer.Count; branch = new BranchInstruction(condition, branchIndex+1, -1); buffer.Add(branch); }
public bool ElseIf(Expression condition) { return false; }