public override EvalResult Evaluate(MakeState s) { /* First, parse variables in the shell command */ StringBuilder sb = new StringBuilder(); int i = 0; while (i < val.Length) { if (val[i] == '\\' && i < (val.Length - 1)) { sb.Append(val[i++]); sb.Append(val[i++]); } else if (val[i] == '$') { /* Now try and match the largest possible variable we can */ int j = i + 1; string match = null; while (j < (val.Length) && val[j] != '$') { string test = val.Substring(i + 1, j - i); if (s.IsDefined(test)) { match = test; } j++; } if (match != null) { sb.Append(s.GetDefine(match).strval); i++; i += match.Length; } else { sb.Append(val[i++]); } } else { sb.Append(val[i++]); } } return(new EvalResult(sb.ToString())); }
public override Expression.EvalResult Execute(MakeState s) { if (s.IsDefined(v) == false) { throw new Exception("export: variable " + v + " is not defined in this scope"); } Expression.EvalResult e = s.GetDefine(v); s.SetDefine(v, e); MakeState cur_s = s.parent; while (cur_s != null) { cur_s.SetDefine(v, e); cur_s = cur_s.parent; } return(new Expression.EvalResult(0)); }
public override EvalResult Evaluate(MakeState s) { if (s.IsDefined(val)) { return(s.GetDefine(val)); } else if (s.funcs.ContainsKey(val)) { return new EvalResult { Type = EvalResult.ResultType.Function, funcval = new FunctionStatement { name = val } } } ; else { return(new EvalResult()); } }
public virtual EvalResult Evaluate(MakeState s) { EvalResult ea, eb; switch (op) { case Tokens.NOT: ea = a.Evaluate(s); check_null(ea); if (ea.AsInt == 0) { return(new EvalResult(1)); } else { return(new EvalResult(0)); } case Tokens.LAND: ea = a.Evaluate(s); if (ea.AsInt == 0) { return(new EvalResult(0)); } eb = b.Evaluate(s); if (eb.AsInt == 0) { return(new EvalResult(0)); } return(new EvalResult(1)); case Tokens.LOR: ea = a.Evaluate(s); if (ea.AsInt != 0) { return(new EvalResult(1)); } eb = b.Evaluate(s); if (eb.AsInt != 0) { return(new EvalResult(1)); } return(new EvalResult(0)); case Tokens.PLUS: ea = a.Evaluate(s); eb = b.Evaluate(s); check_null(ea); check_null(eb); if (ea.Type == EvalResult.ResultType.Int && eb.Type == EvalResult.ResultType.Int) { return(new EvalResult(ea.intval + eb.intval)); } else if (ea.Type == EvalResult.ResultType.String && eb.Type == EvalResult.ResultType.String) { return(new EvalResult(ea.strval + eb.strval)); } else if (ea.Type == EvalResult.ResultType.Int && eb.Type == EvalResult.ResultType.String) { return(new EvalResult(ea.intval.ToString() + eb.strval)); } else if (ea.Type == EvalResult.ResultType.String && eb.Type == EvalResult.ResultType.Int) { return(new EvalResult(ea.strval + eb.intval.ToString())); } else if (ea.Type == EvalResult.ResultType.Void && eb.Type == EvalResult.ResultType.Void) { return(new EvalResult()); } else { throw new Statement.SyntaxException("Mismatched arguments to PLUS: " + ea.Type.ToString() + " and " + eb.Type.ToString()); } case Tokens.MUL: ea = a.Evaluate(s); eb = b.Evaluate(s); check_null(ea); check_null(eb); return(new EvalResult(ea.AsInt * eb.AsInt)); case Tokens.MINUS: ea = a.Evaluate(s); check_null(ea); if (b == null) { // unary minus if (ea.Type == EvalResult.ResultType.Void) { return(ea); } if (ea.Type == EvalResult.ResultType.String) { throw new Statement.SyntaxException("Cannot apply unary minus to type string"); } return(new EvalResult(0 - ea.intval)); } else { eb = b.Evaluate(s); check_null(eb); if (ea.Type == EvalResult.ResultType.String && (eb.Type == EvalResult.ResultType.Int || eb.Type == EvalResult.ResultType.Void)) { long rem_amount = eb.AsInt; if (rem_amount > ea.strval.Length) { rem_amount = ea.strval.Length; } return(new EvalResult(ea.strval.Substring(0, ea.strval.Length - (int)rem_amount))); } else if (ea.Type == EvalResult.ResultType.String && eb.Type == EvalResult.ResultType.String) { if (ea.strval.EndsWith(eb.strval)) { return(new EvalResult(ea.strval.Substring(0, ea.strval.Length - eb.strval.Length))); } else { throw new Statement.SyntaxException(ea.strval + " does not end with " + eb.strval); } } else if (ea.Type == EvalResult.ResultType.Void && eb.Type == EvalResult.ResultType.Void) { return(new EvalResult()); } else { return(new EvalResult(ea.AsInt - eb.AsInt)); } } case Tokens.DEFINED: ea = a.Evaluate(s); if (ea.Type != EvalResult.ResultType.String) { throw new Statement.SyntaxException("defined requires a string/label argument"); } if (s.IsDefined(ea.strval)) { return(new EvalResult(1)); } else { return(new EvalResult(0)); } case Tokens.EQUALS: case Tokens.NOTEQUAL: { int _true = 1; int _false = 0; if (op == Tokens.NOTEQUAL) { _true = 0; _false = 1; } ea = a.Evaluate(s); eb = b.Evaluate(s); if (ea.Type == EvalResult.ResultType.String && eb.Type == EvalResult.ResultType.String) { if (ea.strval == null) { if (eb.strval == null) { return(new EvalResult(_true)); } else { return(new EvalResult(_false)); } } if (ea.strval.Equals(eb.strval)) { return(new EvalResult(_true)); } else { return(new EvalResult(_false)); } } else if (ea.Type == EvalResult.ResultType.Null && eb.Type != EvalResult.ResultType.Null) { return(new EvalResult(_false)); } else if (ea.Type != EvalResult.ResultType.Null && eb.Type == EvalResult.ResultType.Null) { return(new EvalResult(_false)); } else if (ea.Type == EvalResult.ResultType.Null && eb.Type == EvalResult.ResultType.Null) { return(new EvalResult(_true)); } { if (ea.AsInt == eb.AsInt) { return(new EvalResult(_true)); } else { return(new EvalResult(_false)); } } } case Tokens.LT: ea = a.Evaluate(s); eb = b.Evaluate(s); check_null(ea); check_null(eb); if (ea.AsInt < eb.AsInt) { return(new EvalResult(1)); } else { return(new EvalResult(0)); } case Tokens.GT: ea = a.Evaluate(s); eb = b.Evaluate(s); check_null(ea); check_null(eb); if (ea.AsInt > eb.AsInt) { return(new EvalResult(1)); } else { return(new EvalResult(0)); } case Tokens.LSHIFT: ea = a.Evaluate(s); eb = b.Evaluate(s); check_null(ea); check_null(eb); return(new EvalResult(ea.AsInt << (int)eb.AsInt)); case Tokens.RSHIFT: ea = a.Evaluate(s); eb = b.Evaluate(s); check_null(ea); check_null(eb); return(new EvalResult(ea.AsInt >> (int)eb.AsInt)); case Tokens.OR: ea = a.Evaluate(s); eb = b.Evaluate(s); check_null(ea); check_null(eb); return(new EvalResult(ea.AsInt | eb.AsInt)); case Tokens.AND: ea = a.Evaluate(s); eb = b.Evaluate(s); check_null(ea); check_null(eb); return(new EvalResult(ea.AsInt & eb.AsInt)); } throw new NotImplementedException(op.ToString()); }