KumiScript/interpreter/expression/ProperListExpression.cs

65 lines
No EOL
1.8 KiB
C#

namespace KumiScript.Interpreter
{
public class ProperListExpression : Expression
{
readonly List<Expression> _values;
public ProperListExpression(List<Expression> expressions)
{
_values = expressions;
}
public override Expression Eval(Environment env)
{
Expression rator = Car().Eval(env);
Expression rand = Cdr();
return rator.Apply(rand, env);
}
public override List<Expression> EvalMembers(Environment env)
{
List<Expression> result = new List<Expression>();
for (int i = 0; i < _values.Count; i++)
result.Add(_values[i].Eval(env));
return result;
}
public override List<Expression> GetMembers()
{
return _values;
}
public override string ToString()
{
return string.Concat("(",
string.Join(' ', (object[]) _values.ToArray()),
")");
}
public override Expression Car()
{
return _values.First();
}
public override Expression Cdr()
{
List<Expression> rest = new List<Expression>(_values);
rest.RemoveAt(0);
return ListFactory.MakeList(rest);
}
public override ProperListExpression Cons(Expression expr)
{
List<Expression> consd = new List<Expression>(_values);
consd.Insert(0, expr);
return new ProperListExpression(consd);
}
public override ProperListExpression AppendToList(Expression expr)
{
List<Expression> appended = new List<Expression>(_values);
appended.Append(expr);
return new ProperListExpression(appended);
}
}
}