コード例 #1
0
        public CodeGen(ASTNodeBase astTree, GlobalSymbolTable globalSymbolTable, CodeWriter codeStream)
        {
            _astTree           = astTree;
            _globalSymbolTable = globalSymbolTable;
            _writer            = codeStream;

            _sizeCalculator = new SizeCalculator(_astTree, _globalSymbolTable);
        }
コード例 #2
0
 public EventEditorContext(EditorWindow window, OverviewEditorContext overviewContext, TriggerRoot triggerRoot, List <Trigger> triggers, GlobalSymbolTable symbolTable)
 {
     _triggerRoot     = triggerRoot;
     _overviewContext = overviewContext;
     _window          = window;
     _triggers        = new TriggerCollection(this, _overviewContext, _triggerRoot);
     _symbolTable     = symbolTable;
 }
コード例 #3
0
    public ExecutionContext(ExecutionContext other)
    {
        _globalSymbolTable = other._globalSymbolTable;

        foreach (var localSymbol in other._localSymbolTable)
        {
            _localSymbolTable.Add(localSymbol.Key, localSymbol.Value);
        }
    }
コード例 #4
0
    private void Initialize()
    {
        Cursor.lockState = CursorLockMode.None;
        if (_initialized)
        {
            return;
        }
        _initialized = true;

        _globalSymbolTable = GetComponentInParent <GlobalSymbolTableAccessor>().GlobalSymbolTable;
    }
コード例 #5
0
        //=================================================================

        static object ForceEval(Env env, object exp)
        {
            IASTNode node = Compile(null, TransformLibraryForm(exp));

            GlobalEnv.Instance().ExtendTo(GlobalSymbolTable.Instance().GetSymbolCount());

            KeyValuePair <object, Continue> p = node.Eval(env, (v => new KeyValuePair <object, Continue>(v, null)));;

            while (p.Value != null)
            {
                p = p.Value(p.Key);
            }
            return(p.Key);
        }
コード例 #6
0
 public CodeGenerator(TextWriter sink, Program program, GlobalSymbolTable symbols, DiagnosticsReport diagnostics, NativeDB nativeDB)
 {
     Sink             = sink;
     Program          = program;
     Symbols          = symbols;
     Diagnostics      = diagnostics;
     NativeDB         = nativeDB;
     Strings          = new();
     stmtEmitter      = new(this);
     valueEmitter     = new(this);
     addressEmitter   = new(this);
     optimizer        = new();
     funcInstructions = new();
 }
コード例 #7
0
        public CodeGeneratorVisitor(CodeWriter writer, GlobalSymbolTable table)
        {
            _writer            = writer;
            _globalSymbolTable = table;

            _availableRegisters = new Stack <string>();
            _branchCounter      = 0;
            _writeLabelCounter  = 0;
            _readLabelCounter   = 0;

            _availableRegisters.Push(Registers.R11);
            _availableRegisters.Push(Registers.R10);
            _availableRegisters.Push(Registers.R9);
            _availableRegisters.Push(Registers.R8);
            _availableRegisters.Push(Registers.R7);
            _availableRegisters.Push(Registers.R6);
            _availableRegisters.Push(Registers.R5);
            _availableRegisters.Push(Registers.R4);
            _availableRegisters.Push(Registers.R3);
            _availableRegisters.Push(Registers.R2);
            _availableRegisters.Push(Registers.R1);
        }
コード例 #8
0
 public (float X, float Y, float Z) EvalVector(InvocationExpression expr, GlobalSymbolTable symbols) => ((BasicIntrinsicType)Type).EvalVector(expr, symbols);
コード例 #9
0
 public static float EvalFloat(IExpression expression, GlobalSymbolTable symbols)
 => expression.Accept(new FloatEvaluator(symbols), default);
コード例 #10
0
 public static (float X, float Y, float Z) EvalVector(IExpression expression, GlobalSymbolTable symbols)
 => expression.Accept(new VectorEvaluator(symbols), default);
コード例 #11
0
 public SizeCalculator(ASTNodeBase astTree, GlobalSymbolTable globalSymbolTable)
 {
     _astTree           = astTree;
     _globalSymbolTable = globalSymbolTable;
 }
コード例 #12
0
        public SymbolTableVisitor(StreamWriter errorStream)
        {
            _errorStream = errorStream;

            GlobalSymbolTable = new GlobalSymbolTable(errorStream);
        }
コード例 #13
0
 public static int GetTypeFullSize(GlobalSymbolTable globalSymbolTable, (string type, List <int> dims) type)
コード例 #14
0
        static void SetupGlobalEnv()
        {
            Dictionary <string, object> builtinVars = new Dictionary <string, object>()
            {
                { "true", true },
                { "false", false },
                { "else", true },
                { "null", null },
            };

            Dictionary <string, Procedure> builtinProcedures = new Dictionary <string, Procedure>()
            {
                { "not", (args, k) => k(!(bool)args[0]) },
                { "identity", (args, k) => k(args[0]) },
                { "sqr", (args, k) => {
                      if (args[0] is BigInteger)
                      {
                          var a = (BigInteger)args[0]; return(k(a * a));
                      }
                      else
                      {
                          var a = (decimal)args[0]; return(k(a * a));
                      }
                  } },
                { "+", (args, k) => {
                      if (args[0] is decimal || args[1] is decimal)
                      {
                          return(k(CastToDecimal(args[0]) + CastToDecimal(args[1])));
                      }
                      return(k((BigInteger)args[0] + (BigInteger)args[1]));
                  } },
                { "-", (args, k) => {
                      if (args[0] is decimal || args[1] is decimal)
                      {
                          return(k(CastToDecimal(args[0]) - CastToDecimal(args[1])));
                      }
                      return(k((BigInteger)args[0] - (BigInteger)args[1]));
                  } },
                { "*", (args, k) => {
                      if (args[0] is decimal || args[1] is decimal)
                      {
                          return(k(CastToDecimal(args[0]) * CastToDecimal(args[1])));
                      }
                      return(k((BigInteger)args[0] * (BigInteger)args[1]));
                  } },
                { "/", (args, k) => {
                      if (args[0] is decimal || args[1] is decimal)
                      {
                          return(k(CastToDecimal(args[0]) / CastToDecimal(args[1])));
                      }
                      return(k((BigInteger)args[0] / (BigInteger)args[1]));
                  } },
                { "quotient", (args, k) => {
                      if (args[0] is decimal || args[1] is decimal)
                      {
                          return(k((BigInteger)(CastToDecimal(args[0]) / CastToDecimal(args[1]))));
                      }
                      return(k((BigInteger)args[0] / (BigInteger)args[1]));
                  } },
                { "remainder", (args, k) => {
                      if (args[0] is decimal || args[1] is decimal)
                      {
                          return(k(CastToDecimal(args[0]) % CastToDecimal(args[1])));
                      }
                      return(k((BigInteger)args[0] % (BigInteger)args[1]));
                  } },
                { "=", (args, k) => k(args[0].Equals(args[1])) },
                { "<", (args, k) => k((args[0] as IComparable).CompareTo(args[1]) < 0) },
                { "<=", (args, k) => k((args[0] as IComparable).CompareTo(args[1]) <= 0) },
                { ">", (args, k) => k((args[0] as IComparable).CompareTo(args[1]) > 0) },
                { ">=", (args, k) => k((args[0] as IComparable).CompareTo(args[1]) >= 0) },
                { "eq?", (args, k) => k(object.ReferenceEquals(args[0], args[1])) },

                { "cons", (args, k) => k(new Pair()
                    {
                        Car = args[0], Cdr = args[1]
                    }) },
                { "car", (args, k) => k(((Pair)args[0]).Car) },
                { "cdr", (args, k) => k(((Pair)args[0]).Cdr) },
                { "drop", (args, k) => {
                      Pair l = (Pair)args[0]; int n = (int)(BigInteger)args[1];
                      for (; n > 0; --n)
                      {
                          l = (Pair)l.Cdr;
                      }
                      return(k(l));
                  } },
                { "length", (args, k) => {
                      int n = 0;
                      for (Pair l = (Pair)args[0]; l != null; ++n, l = (Pair)l.Cdr)
                      {
                          ;
                      }
                      return(k(n));
                  } },
                { "append", (args, k) => {
                      var l = PairToList((Pair)args[0]);
                      l.InsertRange(l.Count, PairToList((Pair)args[1]));
                      return(k(ListToPair(l)));
                  } },
                { "empty?", (args, k) => k(args[0] == null) },

                { "pretty-print", (args, k) => {
                      PrintPairExp(args[0]);
                      return(k(null));
                  } },
                { "display", (args, k) => {
                      PrintListExp(PairExpToListExp(args[0]));
                      return(k(null));
                  } },
                { "current-inexact-milliseconds", (args, k) => {
                      long now;
                      QueryPerformanceCounter(out now);
                      return(k((decimal)(now - sTimerStart) * 1000 / sTimerFreq));
                  } },
                { "exit", (args, k) => {
                      Environment.Exit(0);
                      return(k(null));
                  } },
                { "random", (args, k) => k((BigInteger)sRandom.Next((int)(BigInteger)args[0])) },
                { "eval", (args, k) => k(ForceEval(null, PairExpToListExp(args[0]))) },
                { "call/cc", (args, k) => {
                      return(((Procedure)args[0])(new List <object>()
                        {
                            (Procedure)((args2, k2) => new KeyValuePair <object, Continue>(args2[0], k)),
                        }, k));
                  } },
            };

            GlobalEnv.Instance().ExtendTo(builtinVars.Count + builtinProcedures.Count);
            foreach (var nameValue in builtinVars)
            {
                int index = GlobalSymbolTable.Instance().LookupOrDefine(nameValue.Key);
                GlobalEnv.Instance()[index] = nameValue.Value;
            }
            foreach (var nameValue in builtinProcedures)
            {
                int index = GlobalSymbolTable.Instance().LookupOrDefine(nameValue.Key);
                GlobalEnv.Instance()[index] = nameValue.Value;
            }
        }
コード例 #15
0
        static IASTNode Compile(SymbolTable symTable, object exp)
        {
            if (exp is string)
            {
                string name = (string)exp;
                if (symTable != null && symTable.Lookup(name) != -1)
                {
                    return(new ASTNode_GetLocalVar()
                    {
                        Index = symTable.Lookup(name)
                    });
                }

                int envIndex = 0;
                for (; symTable != null && symTable.Lookup(name) == -1; symTable = symTable.PrevTalbe, ++envIndex)
                {
                    ;
                }

                if (symTable == null)
                {
                    return(new ASTNode_GetGlobalVar()
                    {
                        Index = GlobalSymbolTable.Instance().LookupOrDefine(name)
                    });
                }
                else
                {
                    return(new ASTNode_GetFreeVar()
                    {
                        EnvIndex = envIndex, Index = symTable.Lookup(name)
                    });
                }
            }
            else if (!(exp is List <object>))
            {
                return(new ASTNode_Literal()
                {
                    Value = exp
                });
            }

            List <object> l = (List <object>)exp;

            switch (l[0] as string)
            {
            case "quote":
                return(new ASTNode_Literal()
                {
                    Value = ListExpToPairExp(l[1])
                });

            case "if":
                return(new ASTNode_If()
                {
                    PredNode = Compile(symTable, l[1]), ThenNode = Compile(symTable, l[2]), ElseNode = Compile(symTable, l[3])
                });

            case "lambda": {
                SymbolTable newSymTable = new SymbolTable(symTable);
                foreach (string name in ((List <object>)l[1]))
                {
                    newSymTable.Define(name);
                }

                List <string> defines = new List <string>();
                FindDefinition(defines, (List <object>)l[2], 1);
                foreach (string name in defines)
                {
                    newSymTable.Define(name);
                }

                return(new ASTNode_Lambda()
                    {
                        LocalVarCount = newSymTable.GetSymbolCount(), BodyNode = Compile(newSymTable, l[2])
                    });
            }

            case "begin":
                return(new ASTNode_Begin()
                {
                    Nodes = l.Skip(1).Select(e => Compile(symTable, e)).ToArray()
                });

            case "define":
                return(Compile(symTable, new List <object>()
                {
                    "set!", l[1], l[2]
                }));

            case "set!": {
                IASTNode right = Compile(symTable, l[2]);

                string name = (string)l[1];
                if (symTable != null && symTable.Lookup(name) != -1)
                {
                    return(new ASTNode_SetLocalVar()
                        {
                            Index = symTable.Lookup(name), RightNode = right
                        });
                }

                int envIndex = 0;
                for (; symTable != null && symTable.Lookup(name) == -1; symTable = symTable.PrevTalbe, ++envIndex)
                {
                    ;
                }

                if (symTable == null)
                {
                    return(new ASTNode_SetGlobalVar()
                        {
                            Index = GlobalSymbolTable.Instance().LookupOrDefine(name), RightNode = right
                        });
                }
                else
                {
                    return(new ASTNode_SetFreeVar()
                        {
                            EnvIndex = envIndex, Index = symTable.Lookup(name), RightNode = right
                        });
                }
            }

            default: {
                return(new ASTNode_Application()
                    {
                        ProcedureNode = Compile(symTable, l[0]), ActualNodes = l.Skip(1).Select(e => Compile(symTable, e)).ToArray()
                    });
            }
            }
        }
コード例 #16
0
 public static bool EvalBool(IExpression expression, GlobalSymbolTable symbols)
 => expression.Accept(new BoolEvaluator(symbols), default);
コード例 #17
0
 public IntEvaluator(GlobalSymbolTable symbols) => Symbols = symbols;
コード例 #18
0
 public void Init(InstDeclController graph)
 {
     _symbolTable = GlobalSymbolTable.GetInstance();
     _typeTable   = TypeTable.GetInstance();
     _graph       = graph;
 }
コード例 #19
0
 public ExecutionContext(GlobalSymbolTable globalSymbolTable)
 {
     _globalSymbolTable = globalSymbolTable;
 }
コード例 #20
0
ファイル: Environment.cs プロジェクト: phisiart/C-Compiler
 private Env2(GlobalSymbolTable globalSymbolTable, Option<FunctionScope> functionScope) {
     this._globalSymbolTable = globalSymbolTable;
     this._functionScope = functionScope;
 }
コード例 #21
0
 public float EvalFloat(InvocationExpression expr, GlobalSymbolTable symbols) => throw new NotImplementedException();
コード例 #22
0
    public void Draw()
    {
        GlobalSymbolTable table = _context.GlobalSymbolTable;

        _variableScrollView = GUILayout.BeginScrollView(_variableScrollView, GUI.skin.box, GUILayout.ExpandHeight(false));
        GUILayout.BeginHorizontal();
        if (GUILayout.Button(_expandedViews.Count > 0 ? "-" : "+", GUILayout.Width(20), GUILayout.Height(20)))
        {
            if (_expandedViews.Count > 0)
            {
                _expandedViews.Clear();
            }
            else
            {
                foreach (Variable variable in table.Variables.Values)
                {
                    _expandedViews.Add(variable);
                }
            }
        }
        GUI.skin.label.fontSize = 18;
        GUILayout.Label("Variables", GUILayout.ExpandHeight(false));
        GUI.skin.label.fontSize = 11;
        GUILayout.EndHorizontal();
        EditorGUILayout.Separator();
        GUILayout.BeginHorizontal();
        GUILayout.Space(10);
        GUILayout.BeginVertical();

        foreach (var item in table.Variables.Where(kvp => kvp.Value == null).ToList())
        {
            table.Variables.Remove(item.Key);
        }

        List <KeyValuePair <string, Variable> > toDelete = new List <KeyValuePair <string, Variable> >();

        foreach (var kvp in table.Variables)
        {
            string   identifier = kvp.Key;
            Variable variable   = kvp.Value;
            GUILayout.BeginHorizontal();
            if (GUILayout.Button(_expandedViews.Contains(variable) ? "-" : "+", GUILayout.Width(20), GUILayout.Height(15)))
            {
                if (_expandedViews.Contains(variable))
                {
                    _expandedViews.Remove(variable);
                }
                else
                {
                    _expandedViews.Add(variable);
                }
            }
            GUILayout.Label(identifier + " (" + variable.GetType().Name + ")", GUILayout.ExpandWidth(false));

            GUILayout.FlexibleSpace();
            if (GUILayout.Button("Delete", GUILayout.Width(50), GUILayout.Height(15)))
            {
                toDelete.Add(kvp);
            }
            GUILayout.EndHorizontal();

            if (_expandedViews.Contains(variable))
            {
                SerializedObject   serializedVariable = new SerializedObject(variable);
                SerializedProperty property           = serializedVariable.GetIterator();
                property.NextVisible(true);
                while (property.NextVisible(false))
                {
                    GUILayout.BeginHorizontal();
                    GUILayout.Space(10);
                    EditorGUILayout.PropertyField(property, true, GUILayout.ExpandWidth(false));
                    serializedVariable.ApplyModifiedProperties();
                    GUILayout.EndHorizontal();
                }
            }
        }
        foreach (var variable in toDelete)
        {
            table.Variables.Remove(variable.Key);
        }
        toDelete.ForEach((v) => ScriptableObject.DestroyImmediate(v.Value));
        GUILayout.EndVertical();
        GUILayout.EndHorizontal();

        EditorGUILayout.Separator();
        GUILayout.EndScrollView();

        // Get all Variable types.
        Dictionary <string, Type> typesByName = Assembly.GetAssembly(typeof(Variable)).GetTypes().
                                                Where((t) => typeof(Variable).IsAssignableFrom(t) && !t.IsAbstract && t != typeof(DynamicVariable)).
                                                ToDictionary((t) => t.Name);

        EditorGUILayout.BeginHorizontal();
        bool validIdentifier     = !string.IsNullOrEmpty(_createVariableIdentifier);
        bool availableIdentifier = !table.Variables.ContainsKey(_createVariableIdentifier);

        if (GUILayout.Button("Create New Variable", GUILayout.ExpandWidth(false)))
        {
            if (validIdentifier && availableIdentifier)
            {
                Variable newVariable = (Variable)ScriptableObject.CreateInstance(typesByName.Keys.ToArray()[_createVariableTypeIndex]);
                table.AddVariable(_createVariableIdentifier, newVariable);

                _createVariableIdentifier = "";
                _createVariableTypeIndex  = 0;
            }
        }
        if (!availableIdentifier)
        {
            GUILayout.Label("Variable identifier already in use!");
        }
        else if (!validIdentifier)
        {
            GUILayout.Label("Invalid variable identifier!");
        }
        EditorGUILayout.EndHorizontal();
        GUILayout.BeginHorizontal();
        GUILayout.Label("Identifier: ");
        _createVariableIdentifier = GUILayout.TextField(_createVariableIdentifier);
        GUILayout.EndHorizontal();


        GUILayout.BeginHorizontal();
        GUILayout.Label("Type: ");
        _createVariableTypeIndex = EditorGUILayout.Popup(_createVariableTypeIndex, typesByName.Keys.ToArray());
        GUILayout.EndHorizontal();
    }
コード例 #23
0
 public string EvalString(InvocationExpression expr, GlobalSymbolTable symbols) => throw new NotImplementedException();
コード例 #24
0
 public float EvalFloat(InvocationExpression expr, GlobalSymbolTable symbols) => ((BasicIntrinsicType)Type).EvalFloat(expr, symbols);
コード例 #25
0
 public static string?EvalString(IExpression expression, GlobalSymbolTable symbols)
 => expression.Accept(new StringEvaluator(symbols), default);
コード例 #26
0
 public int EvalInt(InvocationExpression expr, GlobalSymbolTable symbols)
 {
     return(ExpressionEvaluator.EvalInt(expr.Arguments[1], symbols));
 }
コード例 #27
0
 public bool EvalBool(InvocationExpression expr, GlobalSymbolTable symbols) => ((BasicIntrinsicType)Type).EvalBool(expr, symbols);
コード例 #28
0
 public bool EvalBool(InvocationExpression expr, GlobalSymbolTable symbols) => throw new NotImplementedException();
コード例 #29
0
 public string EvalString(InvocationExpression expr, GlobalSymbolTable symbols) => ((BasicIntrinsicType)Type).EvalString(expr, symbols);
コード例 #30
0
 public (float X, float Y, float Z) EvalVector(InvocationExpression expr, GlobalSymbolTable symbols) => throw new NotImplementedException();
コード例 #31
0
 public static int EvalInt(IExpression expression, GlobalSymbolTable symbols)
 => expression.Accept(new IntEvaluator(symbols), default);