KumiScript/interpreter/MathPrimitives.cs

146 lines
6.4 KiB
C#
Raw Normal View History

using KumiScript.Reader;
2024-02-14 06:20:53 +00:00
namespace KumiScript.Interpreter
{
public class MathPrimitives
{
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++)
{
acc += argl[i].GetValueAsFloat();
2024-02-14 06:20:53 +00:00
}
return new NumberExpression(acc);
2024-02-14 06:20:53 +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();
decimal acc = argl[0].GetValueAsFloat();
2024-02-14 06:20:53 +00:00
if (argl.Count == 1)
return new NumberExpression(1 - acc);
2024-02-14 06:20:53 +00:00
for (int i = 1; i < argl.Count; i++)
{
acc -= argl[i].GetValueAsFloat();
2024-02-14 06:20:53 +00:00
}
return new NumberExpression(acc);
2024-02-14 06:20:53 +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();
decimal f = argl[0].GetValueAsFloat();
2024-02-14 06:20:53 +00:00
for (int i = 1; i < argl.Count; i++)
{
f *= argl[i].GetValueAsFloat();
2024-02-14 06:20:53 +00:00
}
return new NumberExpression(f);
2024-02-14 06:20:53 +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();
return new NumberExpression(argl[0].GetValueAsFloat() / argl[1].GetValueAsFloat());
2024-02-14 06:20:53 +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();
return new NumberExpression(argl[0].GetValueAsFloat() % argl[1].GetValueAsFloat());
2024-02-14 06:20:53 +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();
return argl[0].GetValueAsFloat() > argl[1].GetValueAsFloat() ? TrueExpression.GetInstance() : NilExpression.GetInstance();
2024-02-14 06:20:53 +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();
return argl[0].GetValueAsFloat() < argl[1].GetValueAsFloat() ? TrueExpression.GetInstance() : NilExpression.GetInstance();
2024-02-14 06:20:53 +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();
return argl[0].GetValueAsFloat() <= argl[1].GetValueAsFloat() ? TrueExpression.GetInstance() : NilExpression.GetInstance();
2024-02-14 06:20:53 +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();
return argl[0].GetValueAsFloat() >= argl[1].GetValueAsFloat() ? TrueExpression.GetInstance() : NilExpression.GetInstance();
2024-02-14 06:20:53 +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();
Symbol name = argl[0].GetSymbol();
2024-02-14 06:20:53 +00:00
Expression val = env.Lookup(name);
NumberExpression nump = new NumberExpression(val.GetValueAsFloat() + 1);
2024-02-14 06:20:53 +00:00
env.RedefineSymbol(name, nump);
return nump;
}
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();
Symbol name = argl[0].GetSymbol();
2024-02-14 06:20:53 +00:00
Expression val = env.Lookup(name);
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);
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;
}
}
}