Exemplo n.º 1
0
		private object ExecuteInfix (Element elem) {
			object[] args = CollectArgs (elem.Children);
			double a = (double) args[0];
			double b = (double) args[1];
			switch ((char) elem.Val) {
			case '+':
				return a + b;
			case '-':
				return a - b;
			case '*':
				return a * b;
			case '/':
				return a / b;
			case '^':
				return Math.Pow (a, b);
			case '=':
				return a == b;
			case '<':
				return a < b;
			case '>':
				return a > b;
			default:
				throw new Exception ();
			}
		}
Exemplo n.º 2
0
		private object ExecuteStatement (Element elem) {
			object ret = null;
			object[] args = CollectArgs (elem.Children);
			
			context.CallingEngine = this;
			ret = stores.SendMessage (context, (string) elem.Val, args);
			context.CallingEngine = null;
			return ret;
		}
Exemplo n.º 3
0
		private void CompileBareList (Element elem)
		{
			int i = elem.Children.Count - 1;
			foreach (Element subelem in elem.Children) {
				Compile (subelem);
				if (i > 0)
					writer.Write (", ");
				i--;
			}
		}
Exemplo n.º 4
0
		protected override void CompileStatement (Element elem) {
			statement_level++;

			Type[] arg_types = CollectTypes (elem);
			if (stores.SupportsMessage ((string) elem.Val, arg_types)) {
				CompileTypedStatement (elem, arg_types);
			} else {
				CompileGenericStatement (elem);
			}
			statement_level--;

			if (statement_level == 0) {
				writer.WriteLine (";");
			}
		}
Exemplo n.º 5
0
		protected override void CompileInfix (Element elem) {
			char op = (char) elem.Val;

			if (op == '^') {
				writer.Write ("System.Math.Pow (");
				CompileInfixArg (elem.Children[0]);
				writer.Write (", ");
				CompileInfixArg (elem.Children[1]);
				writer.Write (")");
				return;
			}

			CompileInfixArg (elem.Children[0]);
			if (op == '=')
				writer.Write ("==");
			else
				writer.Write (op);
			CompileInfixArg (elem.Children[1]);
		}
Exemplo n.º 6
0
		public void Compile (Element elem) {
			switch (elem.Type) {
			case ElementType.Literal:
				CompileLiteral (elem);
				break;
			case ElementType.List:
				CompileList (elem);
				break;
			case ElementType.Statement:
				CompileStatement (elem);
				break;
			case ElementType.Infix:
				CompileInfix (elem);
				break;
			case ElementType.Variable:
				CompileVariable (elem);
				break;
			default:
				throw new Exception ();
			}
		}
Exemplo n.º 7
0
		protected abstract void CompileVariable (Element elem);
Exemplo n.º 8
0
		protected abstract void CompileLiteral (Element elem);
Exemplo n.º 9
0
		protected abstract void CompileList (Element elem);
Exemplo n.º 10
0
		protected abstract void CompileInfix (Element elem);
Exemplo n.º 11
0
		protected abstract void CompileStatement (Element elem);	
Exemplo n.º 12
0
		public void Insert (int index, Element elem) {
			List.Insert (index, elem);
		}
Exemplo n.º 13
0
		private Type[] CollectTypes (Element elem) {
			return null;
		}
Exemplo n.º 14
0
		protected override void CompileList (Element elem)
		{
			writer.Write ("new object[] {");
			CompileBareList (elem);
			writer.Write ("}");
		}
Exemplo n.º 15
0
		private void CompileInfixArg (Element elem) {
			writer.Write ("((double) ");
			Compile (elem);
			writer.Write (")");
		}
Exemplo n.º 16
0
		private void CompileTypedStatement (Element elem, Type[] arg_types) {
			writer.Write ("Funcs.{0} (", elem.Val);
			CompileBareList (elem);
			writer.Write (")");
		}
Exemplo n.º 17
0
		private void CompileGenericStatement (Element elem) {
			writer.Write ("_funcs.SendMessage (_context, \"{0}\", ", elem.Val);
			CompileList (elem);
			writer.Write (")");
		}
Exemplo n.º 18
0
		protected override void CompileLiteral (Element elem)
		{
			object val = elem.Val;
			
			if (val is string)
				writer.Write ("\"{0}\"", val);
			else if (val is char)
				writer.Write ("'{0}'", val);
			else
				writer.Write (val);
		}
Exemplo n.º 19
0
		public int Add (Element elem) {
			return List.Add (elem);
		}
Exemplo n.º 20
0
		private object ConstructList (Element elem) {
			object[] list = new object[elem.Children.Count];
			int i = 0;
			foreach (Element subelem in elem.Children) {
				if (subelem.Type == ElementType.List)
					list[i] = ConstructList (subelem);
				else
					list[i] = subelem.Val;
				i++;
			}

			return list;
		}
Exemplo n.º 21
0
		protected override void CompileVariable (Element elem)
		{
			writer.Write ("Funcs.Thing (_context, \"{0}\")", (string) elem.Val);
		}