Exemple #1
0
 protected override object?Visit(Statement.Var var)
 {
     AllocateName(var, Push(var.Name));
     base.Visit(var);
     currentName.Pop();
     return(null);
 }
        public object VisitVarStatement(Statement.Var statement)
        {
            Declare(statement.name);

            if (statement.initialiser != null)
            {
                Resolve(statement.initialiser);
            }

            Define(statement.name);
            return(null);
        }
        public object VisitVarStatement(Statement.Var statement)
        {
            object value = null;

            if (statement.initialiser != null)
            {
                value = Evaluate(statement.initialiser);
            }
            else
            {
                throw new RuntimeError(statement.name, $"Variable '{statement.name.lexeme}' not initialised.");
            }

            environment.Define(statement.name.lexeme, value);

            return(null);
        }
Exemple #4
0
        protected override Value?Visit(Statement.Var var)
        {
            var symbol = (Symbol.Var)SymbolTable.DefinedSymbol(var);

            Debug.Assert(symbol.Type != null);
            var type = symbol.Type;
            // Globals and locals are very different
            Value varSpace;

            if (symbol.Kind == Symbol.VarKind.Global)
            {
                // Global variable
                varSpace = Builder.DefineGlobal(var.Name, TranslateToLirType(type));
                if (var.Value != null)
                {
                    // Assign initial value in the startup code
                    Builder.WithPrelude(b =>
                    {
                        var initialValue = VisitNonNull(var.Value);
                        b.Store(varSpace, initialValue);
                    });
                }
                // Associate with symbol
                globalContext.Variables.Add(symbol, varSpace);
            }
            else
            {
                // Local variable
                // Allocate space
                varSpace = Builder.Alloc(TranslateToLirType(type));
                if (var.Value != null)
                {
                    // We also need to assign the value
                    var value = VisitNonNull(var.Value);
                    Builder.Store(varSpace, value);
                }
                // Associate with symbol
                context.Variables.Add(symbol, varSpace);
            }
            return(null);
        }
Exemple #5
0
        protected override object?Visit(Statement.Var var)
        {
            base.Visit(var);
            var  symbol       = (Symbol.Var)System.SymbolTable.DefinedSymbol(var);
            Type?inferredType = null;

            if (var.Type != null)
            {
                // We have a type declaration
                inferredType = System.EvaluateType(var.Type);
            }
            if (var.Value != null)
            {
                // We have an initializer value
                var valueType = System.TypeOf(var.Value);
                if (inferredType == null)
                {
                    // No declared type
                    inferredType = valueType;
                }
                else
                {
                    // The delared type must match the value type
                    if (!inferredType.Equals(valueType))
                    {
                        System.Report(new TypeMismatchError(inferredType, valueType)
                        {
                            Defined = var.Type?.ParseTreeNode,
                            Wrong   = var.Value.ParseTreeNode,
                            Context = "variable definition",
                        });
                    }
                }
            }
            Debug.Assert(inferredType != null);
            symbol.Type = inferredType;
            return(null);
        }
Exemple #6
0
 protected override object?Visit(Statement.Var var)
 {
     base.Visit(var);
     symbolTable.DefineSymbol(var, new Symbol.Var(var, symbolTable.IsGlobal(var)));
     return(null);
 }
Exemple #7
0
 public Token visitVarStatement(Statement.Var varStmt)
 {
     return(varStmt.name);
 }
Exemple #8
0
 /// <summary>
 /// Initializes a new <see cref="Var"/>.
 /// </summary>
 /// <param name="definition">The variable definition statement.</param>
 /// <param name="isGlobal">True, if this is a global variable.</param>
 public Var(Statement.Var definition, bool isGlobal)
     : this(definition, definition.Name, isGlobal ? VarKind.Global : VarKind.Local)
 {
 }