2024-07-12 00:17:00 +00:00
|
|
|
using KumiScript.Reader;
|
2024-02-14 06:20:53 +00:00
|
|
|
|
|
|
|
namespace KumiScript.Interpreter
|
|
|
|
{
|
|
|
|
public class MathPrimitives
|
|
|
|
{
|
2024-07-12 00:17:00 +00:00
|
|
|
public static NumberExpression Plus(Expression args, Environment env)
|
2024-02-14 06:20:53 +00:00
|
|
|
{
|
|
|
|
List<Expression> argl = args.EvalMembers(env);
|
|
|
|
decimal acc = 0;
|
|
|
|
for (int i = 0; i < argl.Count; i++)
|
|
|
|
{
|
2024-07-12 00:17:00 +00:00
|
|
|
acc += argl[i].GetValueAsFloat();
|
2024-02-14 06:20:53 +00:00
|
|
|
}
|
|
|
|
|
2024-07-12 00:17:00 +00:00
|
|
|
return new NumberExpression(acc);
|
2024-02-14 06:20:53 +00:00
|
|
|
}
|
|
|
|
|
2024-07-12 00:17:00 +00:00
|
|
|
public static NumberExpression Minus(Expression args, Environment env)
|
2024-02-14 06:20:53 +00:00
|
|
|
{
|
|
|
|
List<Expression> argl = args.EvalMembers(env);
|
|
|
|
if (argl.Count == 0)
|
|
|
|
throw new InterpreterInvalidApplicationException();
|
|
|
|
|
2024-07-12 00:17:00 +00:00
|
|
|
decimal acc = argl[0].GetValueAsFloat();
|
2024-02-14 06:20:53 +00:00
|
|
|
if (argl.Count == 1)
|
2024-07-12 00:17:00 +00:00
|
|
|
return new NumberExpression(1 - acc);
|
2024-02-14 06:20:53 +00:00
|
|
|
|
|
|
|
for (int i = 1; i < argl.Count; i++)
|
|
|
|
{
|
2024-07-12 00:17:00 +00:00
|
|
|
acc -= argl[i].GetValueAsFloat();
|
2024-02-14 06:20:53 +00:00
|
|
|
}
|
2024-07-12 00:17:00 +00:00
|
|
|
return new NumberExpression(acc);
|
2024-02-14 06:20:53 +00:00
|
|
|
}
|
|
|
|
|
2024-07-12 00:17:00 +00:00
|
|
|
public static NumberExpression Times(Expression args, Environment env)
|
2024-02-14 06:20:53 +00:00
|
|
|
{
|
|
|
|
List<Expression> argl = args.EvalMembers(env);
|
|
|
|
if (argl.Count < 2)
|
|
|
|
throw new InterpreterInvalidInvocationException();
|
|
|
|
|
2024-07-12 00:17:00 +00:00
|
|
|
decimal f = argl[0].GetValueAsFloat();
|
2024-02-14 06:20:53 +00:00
|
|
|
for (int i = 1; i < argl.Count; i++)
|
|
|
|
{
|
2024-07-12 00:17:00 +00:00
|
|
|
f *= argl[i].GetValueAsFloat();
|
2024-02-14 06:20:53 +00:00
|
|
|
}
|
2024-07-12 00:17:00 +00:00
|
|
|
return new NumberExpression(f);
|
2024-02-14 06:20:53 +00:00
|
|
|
}
|
|
|
|
|
2024-07-12 00:17:00 +00:00
|
|
|
public static NumberExpression DividedBy(Expression args, Environment env)
|
2024-02-14 06:20:53 +00:00
|
|
|
{
|
|
|
|
List<Expression> argl = args.EvalMembers(env);
|
|
|
|
if (argl.Count != 2)
|
|
|
|
throw new InterpreterInvalidApplicationException();
|
|
|
|
|
2024-07-12 00:17:00 +00:00
|
|
|
return new NumberExpression(argl[0].GetValueAsFloat() / argl[1].GetValueAsFloat());
|
2024-02-14 06:20:53 +00:00
|
|
|
}
|
|
|
|
|
2024-07-12 00:17:00 +00:00
|
|
|
public static NumberExpression Modulus(Expression args, Environment env)
|
2024-02-14 06:20:53 +00:00
|
|
|
{
|
|
|
|
List<Expression> argl = args.EvalMembers(env);
|
|
|
|
if (argl.Count != 2)
|
|
|
|
throw new InterpreterInvalidApplicationException();
|
|
|
|
|
2024-07-12 00:17:00 +00:00
|
|
|
return new NumberExpression(argl[0].GetValueAsFloat() % argl[1].GetValueAsFloat());
|
2024-02-14 06:20:53 +00:00
|
|
|
}
|
|
|
|
|
2024-07-12 00:17:00 +00:00
|
|
|
public static Expression GreaterThan(Expression args, Environment env)
|
2024-02-14 06:20:53 +00:00
|
|
|
{
|
|
|
|
List<Expression> argl = args.EvalMembers(env);
|
|
|
|
if (argl.Count != 2)
|
|
|
|
throw new InterpreterInvalidApplicationException();
|
|
|
|
|
2024-07-12 00:17:00 +00:00
|
|
|
return argl[0].GetValueAsFloat() > argl[1].GetValueAsFloat() ? TrueExpression.GetInstance() : NilExpression.GetInstance();
|
2024-02-14 06:20:53 +00:00
|
|
|
}
|
2024-07-12 00:17:00 +00:00
|
|
|
public static Expression LessThan(Expression args, Environment env)
|
2024-02-14 06:20:53 +00:00
|
|
|
{
|
|
|
|
List<Expression> argl = args.EvalMembers(env);
|
|
|
|
if (argl.Count != 2)
|
|
|
|
throw new InterpreterInvalidApplicationException();
|
|
|
|
|
2024-07-12 00:17:00 +00:00
|
|
|
return argl[0].GetValueAsFloat() < argl[1].GetValueAsFloat() ? TrueExpression.GetInstance() : NilExpression.GetInstance();
|
2024-02-14 06:20:53 +00:00
|
|
|
}
|
|
|
|
|
2024-07-12 00:17:00 +00:00
|
|
|
public static Expression LessThanEquals(Expression args, Environment env)
|
2024-02-14 06:20:53 +00:00
|
|
|
{
|
|
|
|
List<Expression> argl = args.EvalMembers(env);
|
|
|
|
if (argl.Count != 2)
|
|
|
|
throw new InterpreterInvalidApplicationException();
|
|
|
|
|
2024-07-12 00:17:00 +00:00
|
|
|
return argl[0].GetValueAsFloat() <= argl[1].GetValueAsFloat() ? TrueExpression.GetInstance() : NilExpression.GetInstance();
|
2024-02-14 06:20:53 +00:00
|
|
|
}
|
|
|
|
|
2024-07-12 00:17:00 +00:00
|
|
|
public static Expression GreaterThanEquals(Expression args, Environment env)
|
2024-02-14 06:20:53 +00:00
|
|
|
{
|
|
|
|
List<Expression> argl = args.EvalMembers(env);
|
|
|
|
if (argl.Count != 2)
|
|
|
|
throw new InterpreterInvalidApplicationException();
|
|
|
|
|
2024-07-12 00:17:00 +00:00
|
|
|
return argl[0].GetValueAsFloat() >= argl[1].GetValueAsFloat() ? TrueExpression.GetInstance() : NilExpression.GetInstance();
|
2024-02-14 06:20:53 +00:00
|
|
|
}
|
|
|
|
|
2024-07-12 00:17:00 +00:00
|
|
|
public static Expression PlusPlus(Expression args, Environment env)
|
2024-02-14 06:20:53 +00:00
|
|
|
{
|
|
|
|
List<Expression> argl = args.GetMembers();
|
|
|
|
if (argl.Count != 1)
|
|
|
|
throw new InterpreterInvalidApplicationException();
|
|
|
|
|
2024-07-12 00:17:00 +00:00
|
|
|
Symbol name = argl[0].GetSymbol();
|
2024-02-14 06:20:53 +00:00
|
|
|
Expression val = env.Lookup(name);
|
2024-07-12 00:17:00 +00:00
|
|
|
NumberExpression nump = new NumberExpression(val.GetValueAsFloat() + 1);
|
2024-02-14 06:20:53 +00:00
|
|
|
env.RedefineSymbol(name, nump);
|
|
|
|
return nump;
|
|
|
|
}
|
|
|
|
|
2024-07-12 00:17:00 +00:00
|
|
|
public static Expression MinusMinus(Expression args, Environment env)
|
2024-02-14 06:20:53 +00:00
|
|
|
{
|
|
|
|
List<Expression> argl = args.GetMembers();
|
|
|
|
if (argl.Count != 1)
|
|
|
|
throw new InterpreterInvalidApplicationException();
|
|
|
|
|
2024-07-12 00:17:00 +00:00
|
|
|
Symbol name = argl[0].GetSymbol();
|
2024-02-14 06:20:53 +00:00
|
|
|
Expression val = env.Lookup(name);
|
2024-07-12 00:17:00 +00:00
|
|
|
NumberExpression nump = new NumberExpression(val.GetValueAsFloat() - 1);
|
2024-02-14 06:20:53 +00:00
|
|
|
env.RedefineSymbol(name, nump);
|
|
|
|
return nump;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Environment RegisterPrimitives(Environment outer)
|
|
|
|
{
|
|
|
|
Environment result = new Environment(outer);
|
2024-07-12 00:17:00 +00:00
|
|
|
result.AddSymbol(SymbolTable.GetInstance().FromString("+"), new ProcedureExpression(new PrimitiveProcedure(Plus)));
|
|
|
|
result.AddSymbol(SymbolTable.GetInstance().FromString("-"), new ProcedureExpression(new PrimitiveProcedure(Minus)));
|
|
|
|
result.AddSymbol(SymbolTable.GetInstance().FromString("*"), new ProcedureExpression(new PrimitiveProcedure(Times)));
|
|
|
|
result.AddSymbol(SymbolTable.GetInstance().FromString("/"), new ProcedureExpression(new PrimitiveProcedure(DividedBy)));
|
|
|
|
result.AddSymbol(SymbolTable.GetInstance().FromString("%"), new ProcedureExpression(new PrimitiveProcedure(Modulus)));
|
|
|
|
result.AddSymbol(SymbolTable.GetInstance().FromString(">"), new ProcedureExpression(new PrimitiveProcedure(GreaterThan)));
|
|
|
|
result.AddSymbol(SymbolTable.GetInstance().FromString("<"), new ProcedureExpression(new PrimitiveProcedure(LessThan)));
|
|
|
|
result.AddSymbol(SymbolTable.GetInstance().FromString("<="), new ProcedureExpression(new PrimitiveProcedure(LessThanEquals)));
|
|
|
|
result.AddSymbol(SymbolTable.GetInstance().FromString(">="), new ProcedureExpression(new PrimitiveProcedure(GreaterThanEquals)));
|
|
|
|
result.AddSymbol(SymbolTable.GetInstance().FromString("++!"), new ProcedureExpression(new PrimitiveProcedure(PlusPlus)));
|
|
|
|
result.AddSymbol(SymbolTable.GetInstance().FromString("--!"), new ProcedureExpression(new PrimitiveProcedure(MinusMinus)));
|
2024-02-14 06:20:53 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|