KumiScript/interpreter/expression/ProperListExpression.cs

65 lines
1.8 KiB
C#
Raw Normal View History

2024-01-26 05:37:51 +00:00
namespace KumiScript.Interpreter
{
public class ProperListExpression : Expression
2024-01-26 05:37:51 +00:00
{
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();
2024-01-26 05:37:51 +00:00
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()),
2024-01-26 05:37:51 +00:00
")");
}
public override Expression Car()
{
return _values.First();
}
public override Expression Cdr()
2024-01-26 05:37:51 +00:00
{
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)
2024-01-26 05:37:51 +00:00
{
List<Expression> appended = new List<Expression>(_values);
appended.Append(expr);
return new ProperListExpression(appended);
2024-01-26 05:37:51 +00:00
}
}
}