public object Evaluate(Frame environment, Expression expression) { List<Expression> operands = expression.GetRest(); if (operands.Count < 2) { throw new Exception("Invalid number of If arguments"); } Frame evalFrame = new Frame(new Dictionary<string, Procedure>(), environment, environment, null, true, null); object result = evalFrame.Evaluate(operands[0]); bool resultValue = false; if (!bool.TryParse(result.ToString(), out resultValue)) { throw new Exception("If clause not a predicate."); } if (resultValue) { return evalFrame.Evaluate(operands[1],true); } else { if (operands.Count > 2) { return evalFrame.Evaluate(operands[2], true); } else { return string.Empty; } } }
public object Evaluate(Frame environment, Expression expression) { List<Expression> operands = expression.GetRest(); if (operands.Count < 1) { throw new Exception("Invalid number of cond arguments"); } Frame evalFrame = new Frame(new Dictionary<string, Procedure>(0), environment,environment, null, true, null); foreach (Expression expr in operands) { if (expr.GetFirst().ToString() == "else") { return evalFrame.Evaluate(expr.GetRest()[0], true); } object result = evalFrame.Evaluate(expr.GetFirst()); bool resultValue = false; if (!bool.TryParse(result.ToString(), out resultValue)) { throw new Exception("Cond clause not a predicate."); } if (resultValue) { object exprResult = string.Empty; for (int i = 0; i < expr.GetRest().Count;i++ ) { exprResult = evalFrame.Evaluate(expr.GetRest()[i]); } return exprResult; } } return string.Empty; }
public object Evaluate(Frame environment, Expression expression) { List<Expression> operands = expression.GetRest(); if (operands.Count == 0) { throw new Exception("or: invalid number of arguments."); } Frame evalFrame = new Frame(new Dictionary<string, Procedure>(), environment,environment, null, true, null); for (int i = 0; i < operands.Count; i++) { object result = null; if (i == operands.Count - 1) { result = evalFrame.Evaluate(operands[i], true); } else { result = evalFrame.Evaluate(operands[i]); } bool resultValue = false; if (!bool.TryParse(result.ToString(), out resultValue)) { throw new Exception("or: clause not a predicate."); } if (resultValue) { return true; } } return false; }
private void AddDefinition(Frame environment, List<string> paramNames, Dictionary<string, Procedure> bindings, Expression definition) { if (definition != null) { string name = definition.GetFirst().ToString(); paramNames.Add(name); if (definition.GetRest().Count == 0) { throw new Exception("Let: expression missing for " + name); } //TODO clean up the constructor and property Frame valueEnv = new Frame(new Dictionary<string, Procedure>(0), environment, null, new Identity(environment, null), false, "let-value"); object value = valueEnv.Evaluate(definition.GetRest()[0],true); if (value is Procedure) { bindings.Add(name, (Procedure)value); } else { bindings.Add(name, new Identity(environment, value)); } } }
public object Evaluate(Frame environment, Expression expression) { if (expression.GetRest().Count < 2) { throw new Exception("Invalid let definition"); } Expression definitionList = expression.GetRest()[0]; List<string> paramNames = new List<string>(definitionList.GetRest().Count + 1); Dictionary<string, Procedure> bindings = new Dictionary<string, Procedure>(paramNames.Capacity); Expression firstDefinition = definitionList.GetFirst(); AddDefinition(environment, paramNames, bindings, firstDefinition); foreach (Expression definition in definitionList.GetRest()) { AddDefinition(environment, paramNames, bindings, definition); } List<Expression> body = new List<Expression>(expression.GetRest().Count - 1); for (int i = 1; i < expression.GetRest().Count; i++) { body.Add(expression.GetRest()[i]); } Lambda block = new Lambda(environment, paramNames, body); Frame child = new Frame(bindings, environment, environment, block,true,null); return child.Evaluate(block.Body); }
public override object Evaluate(Frame environment) { if (environment == null) { throw new ArgumentNullException("Environment can not be null."); } string path = environment.FindBindingValue(Parameters[0]).ToString(); if (!File.Exists(path)) { throw new Exception("Could not find file: " + path); } using (StreamReader codeReader = new StreamReader(File.Open(path, FileMode.Open))) { Scanner scanner = new Scanner(codeReader); Expression expr = scanner.NextExpression; string evaluation = string.Empty; while (expr != null) { evaluation = environment.Evaluate(expr).ToString(); expr = scanner.NextExpression; } return evaluation; } }
public object Evaluate(Frame environment, Expression expression) { List<Expression> operands = expression.GetRest(); if (operands.Count < 2) { throw new Exception("Invalid define arguments"); } bool isFunctionDeclaration = false; if (operands[0] is Combination) { isFunctionDeclaration = true; } String name = operands[0].GetFirst().ToString(); List<string> parameters = new List<string>(); foreach (Expression expr in operands[0].GetRest()) { parameters.Add(expr.ToString()); } List<Expression> body = new List<Expression>(operands.Count - 1); if (!isFunctionDeclaration) { var definitionValue = environment.Evaluate(operands[1]); if (definitionValue is Lambda) { environment.AddBinding(name, (Lambda)definitionValue); } else { environment.AddBinding(name, new Identity(environment,definitionValue)); } return name; } for (int i = 1; i < operands.Count; i++) { body.Add(operands[i]); } environment.AddBinding(name, new Lambda(environment,parameters, body)); return name; }