示例#1
0
    public void Parse()
    {
        var script = new Script();
        //script.globalEnv = GetGlobalEnv(tokens);

        var functionTokens = SeparateFunctions();
        var boop           = ParseFunctions(functionTokens);

        globalEnv           = new Ast.Environment();
        globalEnv.variables = new List <Variable>();
        InterpretFunction(boop, "main");
    }
示例#2
0
    public void InterpretFunction(List <Function> functions, string name)
    {
        var func = functions.SingleOrDefault(o => o.name == name);

        var currentEnv = new Ast.Environment();

        currentEnv.variables = new List <Variable>();

        foreach (var stat in func.statements)
        {
            if (stat[0].Equals(Keywords.Var))
            {
                if (!(stat[1] is Identifier))
                {
                    throw new FormatException("Expected an identifier after var", null);
                }

                if (!(stat[2].Equals(ArithmeticOperators.Equal)))
                {
                    throw new FormatException("Expected = after identifier", null);
                }

                if ((stat[3] is int))
                {
                    currentEnv.variables.Add(new DoubleVar()
                    {
                        identifier = stat[1] as Identifier, value = (int)stat[3]
                    });
                }
                else if (stat[3].Equals(Parentheses.OpenBr))
                {
                    List <Variable> arr = new List <Variable>();
                    var             i   = 3;
                    while (!stat[i].Equals(Parentheses.CloseBr))
                    {
                        i++;
                    }
                    var content = stat.Skip(4).Take(i - 4).Where(o => !o.Equals(Special.Comma)).Select(o => Convert.ToDouble(o));
                    currentEnv.variables.Add(new ArrayVar()
                    {
                        identifier = stat[1] as Identifier, value = content.ToList()
                    });
                }
            }
            else if (stat[0] is Identifier && stat[1].Equals(ArithmeticOperators.Equal))
            {
                if (!(stat[2] is int))
                {
                    throw new FormatException("Expected an integer after =", null);
                }

                currentEnv.variables.FirstOrDefault(o => o.identifier.name == (stat[0] as Identifier).name).value = (int)stat[2];
            }
            else if (stat[0] is Identifier && (stat[1].Equals(Parentheses.OpenPar)))
            {
                if (!(stat[1].Equals(Parentheses.OpenPar)))
                {
                    throw new FormatException("Invocation parentheses not closed", null);
                }

                InterpretFunction(functions, (stat[0] as Identifier).name);
            }
            else if (stat[0].Equals(Keywords.Print))
            {
                if (stat[1] is Identifier)
                {
                    Console.WriteLine($"Function {name} printed: {currentEnv.variables.FirstOrDefault(o => o.identifier.name == (stat[1] as Identifier).name).value}\n");
                }

                if (stat[1] is int)
                {
                    Console.WriteLine((int)stat[1]);
                }
            }
        }

        Console.WriteLine($"Function {name}, ended. Environment: ");
        foreach (var variable in currentEnv.variables)
        {
            Console.WriteLine($"    {variable.identifier.name} = {variable.value}");
        }
        Console.WriteLine();
    }