Exemplo n.º 1
0
            public static bool Eval(Bits bits, Dictionary <Expr, int> exprMap, Expr e)
            {
                var v = new EvalVisitor {
                    bits    = bits,
                    exprMap = exprMap,
                };

                v.Visit(e);
                return(v.stack.Pop());
            }
Exemplo n.º 2
0
        public StringBuilder Compile()
        {
            Scope           scope      = new Scope(mainScope);
            SinumerikParser mainParser = new SinumerikParser(new CommonTokenStream(mainLexer), null, _errorTextWriter);

            mainParser.BuildParseTree = true;
            IParseTree mainTree = mainParser.parse();
            //    outputTextWriter.Close();
            SymbolVisitor mainSymbolVisitor = new SymbolVisitor(functions);

            mainSymbolVisitor.Visit(mainTree);
            EvalVisitor visitor = new EvalVisitor(scope, functions, _gcodeOutput);

            visitor.Visit(mainTree);

            return(_gcodeOutput);
        }
Exemplo n.º 3
0
        public SLValue Invoke(String functionName, List <ExpressionContext> param, int countArgs, Dictionary <String, Function> functions, Scope scope, StringBuilder gcodeBuffer)
        {
            if (countArgs != this._param.Count)
            {
                throw new Exception("Illegal Function call");
            }
            Scope scopeNext = new Scope(null); // create function scope

            EvalVisitor evalVisitor = new EvalVisitor(scope, functions, null);

            for (int i = 0; i < param.Count; i++)
            {
                if (param[i].ChildCount > 0)
                {
                    SLValue value = evalVisitor.Visit(param[i]);
                    scopeNext.assignParam(this._param[i].GetText(), value);
                }
                else
                {
                    SLValue value = scopeNext.GetDefaultValue(this._paramType[i].GetText());
                    scopeNext.assignParam(this._param[i].GetText(), value);
                }
            }
            EvalVisitor evalVistorNext = new EvalVisitor(scopeNext, functions, gcodeBuffer);

            SLValue ret = SLValue.VOID;

            try
            {
                evalVistorNext.Visit(this.block);
            }
            catch (ReturnValue returnValue)
            {
                ret = returnValue.value;
            }
            catch (Exception ex)
            {
                throw new Exception($"Illegal Function {functionName} call");
            }

            return(ret);
        }
Exemplo n.º 4
0
        public static void Interpret(string input)
        {
            try
            {
                // Add newline to stream
                input += Environment.NewLine;

                // Convert to CharStream
                var stream = new AntlrInputStream(input);

                // Feed into lexer
                var lexer = new EelooLexer(stream);

                // Convert to TokenStream
                var tokens = new CommonTokenStream(lexer);

                // Create parser and feed it tokens
                var parser = new EelooParser(tokens);
                parser.AddErrorListener(new SyntaxErrorListener());

                // Top Level Rule
                var tree = parser.program();

                // Create visitor object
                EvalVisitor evalVisitor = new EvalVisitor();

                // Give public access to the visitor and the global scope
                Interpreter.visitor = evalVisitor;

                // Visit tree
                evalVisitor.Visit(tree);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
            finally { }
        }
Exemplo n.º 5
0
        public SLValue InvokeWithoutArgs(String functionName, Dictionary <String, Function> functions, Scope scope, StringBuilder gcodeBuffer)
        {
            Scope       scopeNext      = new Scope(scope);
            EvalVisitor evalVistorNext = new EvalVisitor(scopeNext, functions, gcodeBuffer);

            SLValue ret = SLValue.VOID;

            try
            {
                evalVistorNext.Visit(this.block);
            }
            catch (ReturnValue returnValue)
            {
                ret = returnValue.value;
            }
            catch (Exception ex)
            {
                throw new Exception($"Illegal Function {functionName} call");
            }

            return(ret);
        }