Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Irony.Parsing;
- using Irony.Interpreter.Ast;
- namespace Irony.Interpreter.nullL
- {
- public class nullLangRuntime : LanguageRuntime
- {
- public nullLangRuntime(LanguageData language)
- : base(language)
- {
- }
- public override void Init()
- {
- base.Init();
- //add built-in methods, special form IIF, import Math and Environment methods
- BuiltIns.AddMethod(BuiltInPrintMethod, "print");
- BuiltIns.AddMethod(BuiltInAddMethod, "add");
- BuiltIns.AddMethod(BuiltInSubtractMethod, "subtract");
- BuiltIns.AddMethod(BuiltInMultiplyMethod, "multiply");
- BuiltIns.AddMethod(BuiltInDivideMethod, "divide");
- BuiltIns.AddMethod(BuiltInPowerMethod, "power");
- BuiltIns.AddMethod(BuiltInCalcLoopMethod, "calcloop");
- BuiltIns.AddSpecialForm(SpecialFormsLibrary.Iif, "if", 3, 3);
- BuiltIns.AddSpecialForm(SpecialFormsLibrary.loop, "loop", 2, 3);
- BuiltIns.ImportStaticMembers(typeof(System.Math));
- BuiltIns.ImportStaticMembers(typeof(Environment));
- }
- //Built-in methods
- private object BuiltInPrintMethod(ScriptThread thread, object[] args)
- {
- string text = string.Empty;
- switch (args.Length)
- {
- case 1:
- text = string.Empty + args[0]; //compact and safe conversion ToString()
- break;
- case 0:
- break;
- default:
- text = string.Join(" ", args);
- break;
- }
- thread.App.WriteLine(text);
- return null;
- }
- private object BuiltInAddMethod(ScriptThread thread, object[] args)
- {
- if (args == null || args.Length <= 1) return null;
- double result = 0;
- try
- {
- result = Convert.ToDouble(args[0]);
- }
- catch (FormatException e)
- {
- Console.WriteLine("Input string is not a sequence of digits.");
- }
- catch (OverflowException e)
- {
- Console.WriteLine("The number cannot fit in an double.");
- }
- double num = 0;
- for (int i = 1; i < args.Length; i++)
- {
- try
- {
- num = Convert.ToDouble(args[i]);
- }
- catch (FormatException e)
- {
- Console.WriteLine("Input string is not a sequence of digits.");
- }
- catch (OverflowException e)
- {
- Console.WriteLine("The number cannot fit in an double.");
- }
- result += num;
- }
- return result;
- }
- private object BuiltInSubtractMethod(ScriptThread thread, object[] args)
- {
- if (args == null || args.Length <= 1) return null;
- double result=0;
- try
- {
- result = Convert.ToDouble(args[0]);
- }
- catch (FormatException e)
- {
- Console.WriteLine("Input string is not a sequence of digits.");
- }
- catch (OverflowException e)
- {
- Console.WriteLine("The number cannot fit in an double.");
- }
- double num = 0;
- for (int i = 1; i < args.Length; i++)
- {
- try
- {
- num = Convert.ToDouble(args[i]);
- }
- catch (FormatException e)
- {
- Console.WriteLine("Input string is not a sequence of digits.");
- }
- catch (OverflowException e)
- {
- Console.WriteLine("The number cannot fit in an double.");
- }
- result -= num;
- }
- return result;
- }
- private object BuiltInMultiplyMethod(ScriptThread thread, object[] args)
- {
- if (args == null || args.Length <= 1) return null;
- double result = 0;
- try
- {
- result = Convert.ToDouble(args[0]);
- }
- catch (FormatException e)
- {
- Console.WriteLine("Input string is not a sequence of digits.");
- }
- catch (OverflowException e)
- {
- Console.WriteLine("The number cannot fit in an double.");
- }
- double num = 0;
- for (int i = 1; i < args.Length; i++)
- {
- try
- {
- num = Convert.ToDouble(args[i]);
- }
- catch (FormatException e)
- {
- Console.WriteLine("Input string is not a sequence of digits.");
- }
- catch (OverflowException e)
- {
- Console.WriteLine("The number cannot fit in an double.");
- }
- if (num == 0) return 0;
- result *= num;
- }
- return result;
- }
- private object BuiltInDivideMethod(ScriptThread thread, object[] args)
- {
- if (args == null || args.Length <= 1) return null;
- double first = 0;
- try
- {
- first = Convert.ToDouble(args[0]);
- }
- catch (FormatException e)
- {
- Console.WriteLine("Input string is not a sequence of digits.");
- }
- catch (OverflowException e)
- {
- Console.WriteLine("The number cannot fit in an double.");
- }
- if (first == 0) return 0;
- double divider = 1;
- double num = 0;
- for (int i = 1; i < args.Length; i++)
- {
- try
- {
- num = Convert.ToDouble(args[i]);
- }
- catch (FormatException e)
- {
- Console.WriteLine("Input string is not a sequence of digits.");
- }
- catch (OverflowException e)
- {
- Console.WriteLine("The number cannot fit in an double.");
- }
- if (num == 0)
- {
- Console.Write("Cannot divide by zero.");
- }
- divider *= num;
- }
- var result = first / divider;
- return result;
- }
- private object BuiltInCalcLoopMethod(ScriptThread thread, object[] args)
- {
- if (args == null || args.Length <= 3) return null;
- int iterations = 0;
- string actions = "";
- double result = 0;
- try
- {
- iterations = Convert.ToInt32(args[0]);
- actions = args[1] as string;
- result = Convert.ToDouble(args[2]);
- }
- catch (FormatException e)
- {
- Console.WriteLine("Bad formats. Required (int,string,double[]).");
- }
- if (!(actions.Length == 1 || actions.Length == args.Length - 3))
- {
- Console.WriteLine("Operators number should be equal to args-1 calcloop(x,\"+-\",args[a,b,c])");
- return -7;
- }
- int op = 0;
- double num = 0;
- for (int i = 0; i < iterations; i++)
- {
- for (int j = 3; j < args.Length; j++)
- {
- try
- {
- num = Convert.ToInt32(args[j]);
- }
- catch (FormatException e)
- {
- return "Bad formats. Required (int,string,double[]).";
- }
- if (actions.Length != 1)
- op = j-3;
- if (actions[op] == '+')
- result += num;
- if (actions[op] == '-')
- result -= num;
- if (actions[op] == '*')
- result *= num;
- if (actions[op] == '/')
- result /= num;
- }
- }
- return result;
- }
- private object BuiltInPowerMethod(ScriptThread thread, object[] args)
- {
- if (args == null || args.Length != 2) return null;
- double x = 0;
- double y = 0;
- try
- {
- x = Convert.ToDouble(args[0]);
- y = Convert.ToDouble(args[1]);
- }
- catch (FormatException e)
- {
- Console.WriteLine("Bad formats. Required (double, double).");
- }
- if (x == 0) return 0;
- if (y == 0) return 1;
- double result = 1;
- for (int i = 0; i < y; i++ )
- {
- result *= x;
- }
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement