Exemplo n.º 1
0
        public override void Execute(VirtualMachine vm, Compiler.Nodes.FunctionCallNode node)
        {
            Variable leftVariable = vm.getVariable(node.Left);
            Variable rightVariable = vm.getVariable(node.Right);

            if(leftVariable.Type == VariableType.Number && rightVariable.Type == VariableType.Number) //values are parsable as numbers.
            {
                int left = int.Parse(leftVariable.Value);
                int right = int.Parse(rightVariable.Value);

                switch(node.Name) {
                    case "AreEqual":
                        vm.Return = new Variable(VariableType.Boolean, (left == right).ToString());
                        break;
                    case "IsSmaller":
                        vm.Return = new Variable(VariableType.Boolean, (left < right).ToString());
                        break;
                    case "IsLarger":
                        vm.Return = new Variable(VariableType.Boolean, (left > right).ToString());
                        break;
                    case "IsSmallerOrEqual":
                        vm.Return = new Variable(VariableType.Boolean, (left <= right).ToString());
                        break;
                    case "IsLargerOrEqual":
                        vm.Return = new Variable(VariableType.Boolean, (left <= right).ToString());
                        break;
                }
            }

            //TODO: compare strings?
        }
Exemplo n.º 2
0
        public override void Execute(VirtualMachine vm, FunctionCallNode node)
        {
            Variable variable1 = vm.getVariable(node.Left);
            Variable variable2 = vm.getVariable(node.Right);

            if (variable1.Type == VariableType.Number && variable2.Type == VariableType.Number)
                vm.Return = new Variable(VariableType.Number,(Int32.Parse(variable1.Value) + Int32.Parse(variable2.Value)).ToString());
            else
                vm.Return = new Variable(variable1.Value + variable2.Value);
        }
Exemplo n.º 3
0
        public override void Execute(VirtualMachine vm, FunctionCallNode node)
        {
            Variable variable1 = vm.getVariable(node.Left);
            Variable variable2 = vm.getVariable(node.Right);

            if (variable1.Type == VariableType.Number && variable2.Type == VariableType.Number)
                vm.Return = new Variable(VariableType.Number, (Int32.Parse(variable1.Value) * Int32.Parse(variable2.Value)).ToString());
            else
                throw new Exception("Cannot multiply strings");
        }
Exemplo n.º 4
0
        public override void Execute(VirtualMachine vm, IList<string> parameterNames)
        {
            //var variable1 = compiler.GetVariable(parameterNames[1]);
            //var variable2 = compiler.GetVariable(parameterNames[2]);

            //             if(variable1.Type == VariableType.NUMBER && variable2.Type == VariableType.NUMBER)
            //                 vm.ReturnValue = (Int32.Parse(variable1.Value) + Int32.Parse(variable2.Value))
            //                                   .ToString();
            //             else
            //                 vm.ReturnValue = variable1.Value + variable2.Value;
        }
Exemplo n.º 5
0
 public NodeVisitor(VirtualMachine vm)
 {
     this.vm = vm;
     commands = new List<BaseCommand>();
     commands.Add(new MinCommand());
     commands.Add(new PlusCommand());
     commands.Add(new DivideCommand());
     commands.Add(new MultiplyCommand());
     commands.Add(new CompareCommand());
     commands.Add(new PrintCommand());
 }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            Parser p = new Parser();
            string[] codeLines = p.readFile("code.ivaron"); //Read code
            LinkedList<Token> list = new LinkedList<Token>();
            try {
                list = Tokenizer.tokenize(codeLines); //Tokenize code
            } catch(CompilerException e) {
                foreach (string error in e.Errors)
                {
                    Console.WriteLine(error);
                }
            }

            if(list.Count > 0) {
                LinkedListNode<Token> token = list.First;
                while(token != null ) {
                    Console.Write(token.Value.value + " ");
                    if(token.Value.value == "{" || token.Value.value == ";") {
                        Console.Write("\n");
                    }
                    token = token.Next;
                }
            }

            if(list.Count > 0) {
                LinkedListNode<Token> token = list.First;
                while(token != null) {
                    Console.Write(token.Value.type + " ");
                    if(token.Value.value == "{" || token.Value.value == ";") {
                        Console.Write("\n");
                    }
                    token = token.Next;
                }
            }

            NodeLinkedList nll = TheCompiler.compile(list.First);

            VirtualMachine vm = new VirtualMachine();
            vm.Run(nll);

            // Keep the console window open in debug mode.
            Console.WriteLine("Press return key to exit.");
            System.Console.ReadKey();
        }
Exemplo n.º 7
0
 public abstract void Execute(VirtualMachine vm, FunctionCallNode node);
Exemplo n.º 8
0
 public override void Execute(VirtualMachine vm, FunctionCallNode node)
 {
     Console.WriteLine(vm.Return.Value);
 }
 public override void Execute(VirtualMachine vm, IList<string> parameters)
 {
     //vm.SetVariable(parameters[1], vm.ReturnValue);
 }