Esempio n. 1
0
		private object[] CollectArgs (InstructionList children) {
			object[] args = new object[children.Count];

			int i = 0;
			foreach (Element subelem in children) {
				object val;
				switch (subelem.Type) {
				case ElementType.Literal:
					val = subelem.Val;
					break;
				case ElementType.Infix:
					val = ExecuteInfix (subelem);
					break;
				case ElementType.List:
					val = ConstructList (subelem);
					break;
				case ElementType.Statement:
					val = ExecuteStatement (subelem);
					break;
				case ElementType.Variable:
					val = Funcs.Thing (context, (string) subelem.Val);
					break;
				default:
					throw new Exception ();
				}

				args[i] = val;
				i++;
			}

			return args;
		}
Esempio n. 2
0
		public void Compile (InstructionList list) {
			CompileBeginUnit ();
			foreach (Element elem in list) {
				Compile (elem);
			}
			CompileFinishUnit ();
		}
Esempio n. 3
0
	public static void PrintTree (InstructionList tree, int indent) {
		foreach (Element elem in tree) {
			Console.WriteLine ("{0}{1}: {2}",
									 new String ('\t', indent),
									 elem.Type,
									 elem.Val);
			if (elem.Children != null) {
				PrintTree (elem.Children, indent + 1);
			}
		}
	}
Esempio n. 4
0
		private Element ParseList (IEnumerator iterator) {
			InstructionList list = new InstructionList ();
			while (iterator.MoveNext ()) {
				Token token = (Token) iterator.Current;
				if (token.Type == TokenType.CloseBracket) 
					break;
				else if (token.Type == TokenType.OpenBracket) 
					list.Add (ParseList (iterator));
				else 	
					list.Add (new Element (ElementType.Literal, token.Val));
			}
			
			return new Element (ElementType.List, null, list);
		}
Esempio n. 5
0
		public object Execute (InstructionList list) {
			foreach (Element elem in list) {
				switch (elem.Type) {
				case ElementType.Literal:
					return elem.Val;
				case ElementType.List:
					return ConstructList (elem);
				case ElementType.Statement:
					ExecuteStatement (elem);
					if (context.StopExecution)
						return context.OutputValue;
					break;
				case ElementType.Infix:
					ExecuteInfix (elem);
					break;
				case ElementType.Variable:
					return Funcs.Thing (context, (string) elem.Val);
				default:
					throw new Exception ();
				}
			}

			return null;
		}
Esempio n. 6
0
		public void Parse (Parser parser)
		{
			if (tree == null)
				tree = parser.Parse (tokens);
		}
Esempio n. 7
0
		public Function (string name, InstructionList tree, ArgumentInfo[] args) {
			this.name = name;
			this.tree = tree;
			this.args = args;
		}
Esempio n. 8
0
		private InstructionList ParseBackwards (TokenList tokens, bool grouped) {
			Stack stack = new Stack ();
			
			for (int i = tokens.Count - 1; i >= 0; i--) {
				Token token = tokens[i];
				
				switch (token.Type) {
				case TokenType.Number:
				case TokenType.String:
					stack.Push (new Element (ElementType.Literal, token.Val));
					break;
				case TokenType.PlaceholderElement:
					stack.Push (token.Val);
					break;
				case TokenType.PlaceholderGroup:
					InstructionList group = ParseBackwards ((TokenList) token.Val, true);
					if (group.Count == 1) {
						stack.Push (group[0]);
					} else if (group.Count == 0) {
					} else {
						throw new Exception ("Unexpected grouping");
					}
					break;
				case TokenType.Word:
				case TokenType.Infix:
				case TokenType.Minus:
					InstructionList inner = new InstructionList ();

					int count;
					ElementType etype;
					if (token.Type == TokenType.Word) {
						count = CountArgs ((string) token.Val, grouped && i == 0);
						if (count == -1)
							count = stack.Count;
						etype = ElementType.Statement;
					} else if (token.Type == TokenType.Minus) {
						count = 1;
						etype = ElementType.Statement;
						token.Val = "Minus";
					} else {
						count = 2;
						etype = ElementType.Infix;
					}
					
					for (int j = 0; j < count; j++) 
						inner.Add ((Element) stack.Pop ());

					stack.Push (new Element (etype, token.Val, inner));
					break;
				case TokenType.Newline:
					break;
				case TokenType.Variable:
					stack.Push (new Element (ElementType.Variable, token.Val));
					break;
				case TokenType.QuestionMark:
					// Don't need to check AllowQuestionMark here as the
					// tokenizer checks already,
					stack.Push (new Element (ElementType.QuestionMark, token.Val));
					break;
				case TokenType.PlaceholderFunction:
				default:
					throw new Exception ("Unexpected token: " + token.Type + "<" + token.Val + ">");
				}
			}

			InstructionList tree = new InstructionList ();
			object[] toplevel = stack.ToArray ();
			foreach (object o in toplevel) {
				tree.Add ((Element) o);
			}

			return tree;
		}
Esempio n. 9
0
        private InstructionList ParseBackwards(TokenList tokens, bool grouped)
        {
            Stack stack = new Stack();

            for (int i = tokens.Count - 1; i >= 0; i--)
            {
                Token token = tokens[i];

                switch (token.Type)
                {
                case TokenType.Number:
                case TokenType.String:
                    stack.Push(new Element(ElementType.Literal, token.Val));
                    break;

                case TokenType.PlaceholderElement:
                    stack.Push(token.Val);
                    break;

                case TokenType.PlaceholderGroup:
                    InstructionList group = ParseBackwards((TokenList)token.Val, true);
                    if (group.Count == 1)
                    {
                        stack.Push(group[0]);
                    }
                    else if (group.Count == 0)
                    {
                    }
                    else
                    {
                        throw new Exception("Unexpected grouping");
                    }
                    break;

                case TokenType.Word:
                case TokenType.Infix:
                case TokenType.Minus:
                    InstructionList inner = new InstructionList();

                    int         count;
                    ElementType etype;
                    if (token.Type == TokenType.Word)
                    {
                        count = CountArgs((string)token.Val, grouped && i == 0);
                        if (count == -1)
                        {
                            count = stack.Count;
                        }
                        etype = ElementType.Statement;
                    }
                    else if (token.Type == TokenType.Minus)
                    {
                        count     = 1;
                        etype     = ElementType.Statement;
                        token.Val = "Minus";
                    }
                    else
                    {
                        count = 2;
                        etype = ElementType.Infix;
                    }

                    for (int j = 0; j < count; j++)
                    {
                        inner.Add((Element)stack.Pop());
                    }

                    stack.Push(new Element(etype, token.Val, inner));
                    break;

                case TokenType.Newline:
                    break;

                case TokenType.Variable:
                    stack.Push(new Element(ElementType.Variable, token.Val));
                    break;

                case TokenType.QuestionMark:
                    // Don't need to check AllowQuestionMark here as the
                    // tokenizer checks already,
                    stack.Push(new Element(ElementType.QuestionMark, token.Val));
                    break;

                case TokenType.PlaceholderFunction:
                default:
                    throw new Exception("Unexpected token: " + token.Type + "<" + token.Val + ">");
                }
            }

            InstructionList tree = new InstructionList();

            object[] toplevel = stack.ToArray();
            foreach (object o in toplevel)
            {
                tree.Add((Element)o);
            }

            return(tree);
        }
Esempio n. 10
0
		private void Compile (InstructionList tree) {
			Compiler compiler = Compiler.Create (funcs);
			compiler.Compile (tree);
		}
Esempio n. 11
0
		private void Interpret (InstructionList tree) {
			Interpreter interp = new Interpreter (funcs);
			interp.Execute (tree);	
		}
Esempio n. 12
0
 public Function(string name, InstructionList tree, ArgumentInfo[] args)
 {
     this.name = name;
     this.tree = tree;
     this.args = args;
 }
Esempio n. 13
0
        private void Compile(InstructionList tree)
        {
            Compiler compiler = Compiler.Create(funcs);

            compiler.Compile(tree);
        }
Esempio n. 14
0
        private void Interpret(InstructionList tree)
        {
            Interpreter interp = new Interpreter(funcs);

            interp.Execute(tree);
        }