Beispiel #1
0
 public FunctionCompiler(Compiler compiler, Function func, DataType parameterizedParent, AstScope body)
     : this(compiler)
 {
     Function  = func;
     Namescope = func.GenericType ?? parameterizedParent;
     Body      = body;
 }
Beispiel #2
0
 public AstFinalizer(Source src, string comment, IReadOnlyList <AstAttribute> attrs, Modifiers modifiers, string cond, IReadOnlyList <AstParameter> paramList, AstScope body)
     : base(comment, attrs, modifiers, cond)
 {
     Source       = src;
     Parameters   = paramList;
     OptionalBody = body;
 }
Beispiel #3
0
 public FunctionCompiler(Compiler compiler, Function func, DataType parameterizedParent, AstScope declarationBody)
     : this(compiler)
 {
     Function        = func;
     Namescope       = (func as Method)?.GenericType ?? parameterizedParent;
     DeclarationBody = declarationBody;
 }
Beispiel #4
0
    public void Mangle(AstToplevel topLevel)
    {
        var output = new OutputContext(_outputOptions);

        output.InitializeForFrequencyCounting();
        if (_options.FrequencyCounting)
        {
            topLevel.Print(output);
        }
        _options.Chars = output.FinishFrequencyCounting();
        Walk(topLevel);
        foreach (var s in _options.Reserved)
        {
            var idx = AstScope.Debase54(_options.Chars, s);
            if (idx >= 0)
            {
                _options.ReservedOrIdentifier.Add((uint)idx);
            }
        }
        foreach (var s in OutputContext.ReservedWords)
        {
            var idx = AstScope.Debase54(_options.Chars, s);
            if (idx >= 0)
            {
                _options.ReservedOrIdentifier.Add((uint)idx);
            }
        }
        for (var i = 0u; i < _toMangle.Count; i++)
        {
            _toMangle[i].Mangle(_options);
        }
    }
Beispiel #5
0
 public AstConverter(string comment, IReadOnlyList <AstAttribute> attrs, Modifiers modifiers, string cond, AstExpression targetTypeExpression, IReadOnlyList <AstParameter> argList, AstScope body)
     : base(comment, attrs, modifiers, cond)
 {
     TargetType   = targetTypeExpression;
     Parameters   = argList;
     OptionalBody = body;
 }
Beispiel #6
0
 public AstAccessor(Source src, Modifiers modifiers, string cond, AstScope body)
     : base(src)
 {
     Modifiers         = modifiers;
     OptionalCondition = cond;
     OptionalBody      = body;
 }
Beispiel #7
0
 public AstMethod(string comment, IReadOnlyList <AstAttribute> attrs, Modifiers modifiers, string cond, AstExpression dataType, AstExpression optionalInterfaceType, AstIdentifier name, IReadOnlyList <AstParameter> argList, AstGenericSignature optionalGenericSig, AstScope body)
     : base(comment, attrs, modifiers, cond, dataType, name)
 {
     OptionalInterfaceType    = optionalInterfaceType;
     Parameters               = argList;
     OptionalGenericSignature = optionalGenericSig;
     OptionalBody             = body;
 }
Beispiel #8
0
        protected override void Visit(AstNode node)
        {
            switch (node)
            {
            case AstLabeledStatement _:
            {
                // _labelIndex is incremented when we get to the AstLabel
                var saveLabelIndex = _labelIndex;
                DescendOnce();
                _labelIndex = saveLabelIndex;
                break;
            }

            case AstScope scope:
            {
                foreach (var def in scope.Variables.Values)
                {
                    if (_options.Reserved.Contains(def.Name))
                    {
                        continue;
                    }
                    _toMangle.Add(def);
                }

                break;
            }

            case IMayBeBlockScope blockScope when blockScope.IsBlockScope:
            {
                foreach (var def in blockScope.BlockScope.Variables.Values)
                {
                    if (_options.Reserved.Contains(def.Name))
                    {
                        continue;
                    }
                    _toMangle.Add(def);
                }

                break;
            }

            case AstLabel label:
            {
                string name;
                do
                {
                    name = AstScope.Base54(_options.Chars, _labelIndex++);
                }while (!OutputContext.IsIdentifier(name));
                label.MangledName = name;
                StopDescending();
                break;
            }

            case AstSymbolCatch symbolCatch:
                _toMangle.Add(symbolCatch.Thedef);
                break;
            }
        }
Beispiel #9
0
 public AstOperator(Source src, string comment, IReadOnlyList <AstAttribute> attrs, Modifiers modifiers, string cond, AstExpression returnType, OperatorType op, IReadOnlyList <AstParameter> argList, AstScope body)
     : base(comment, attrs, modifiers, cond)
 {
     Source       = src;
     ReturnType   = returnType;
     Operator     = op;
     Parameters   = argList;
     OptionalBody = body;
 }
Beispiel #10
0
 public AstConstructor(Source src, string comment, IReadOnlyList <AstAttribute> attributes, Modifiers modifiers, string cond, IReadOnlyList <AstParameter> paramList,
                       AstConstructorCallType callType = 0, IReadOnlyList <AstArgument> callArgs = null, AstScope body = null)
     : base(comment, attributes, modifiers, cond)
 {
     Source        = src;
     CallType      = callType;
     CallArguments = callArgs;
     Parameters    = paramList;
     OptionalBody  = body;
 }
Beispiel #11
0
 public SymbolDef(AstScope scope, AstSymbol orig, AstNode?init)
 {
     Name  = orig.Name;
     Scope = scope;
     Orig  = new StructList <AstSymbol>();
     Orig.Add(orig);
     Init        = init;
     References  = new StructList <AstSymbol>();
     Global      = false;
     MangledName = null;
     MangledIdx  = -2;
     Undeclared  = false;
     Defun       = null;
 }
Beispiel #12
0
        AstScope ProcessScopeNode(AstScope astScope)
        {
            if (_isInScope)
            {
                return(astScope);
            }

            _isInScope = true;
            Descend();
            _isInScope = false;

            if (_astVarCount == 0 || _astVarCount == 1 && !_canPerformMergeDefAndInit && astScope.Body[0] is AstVar)
            {
                return(astScope);
            }

            return(HoistVariables(astScope));
        }
Beispiel #13
0
        public void Mangle(ScopeOptions options)
        {
            if (MangledName != null)
            {
                return;
            }
            if (Unmangleable(options))
            {
                if (!options.IgnoreEval && Scope.Pinned())
                {
                    var mangledIdx = AstScope.Debase54(options.Chars, Name);
                    var enc        = Scope.Enclosed.AsReadOnlySpan();
                    foreach (var encSym in enc)
                    {
                        if (encSym.MangledIdx == mangledIdx && encSym.MangledName != null)
                        {
                            encSym.MangledName = null;
                            encSym.Mangle(options);
                        }
                    }
                }
                return;
            }
            var def = Redefined();

            if (def != null)
            {
                if (def.MangledIdx >= 0)
                {
                    MangledName = def.MangledName;
                    MangledIdx  = def.MangledIdx;
                }
                else
                {
                    MangledName = def.Name;
                    MangledIdx  = AstScope.Debase54(options.Chars, MangledName);
                }
            }
            else
            {
                (MangledName, MangledIdx) = ((string, int))Scope.NextMangled(options, this);
            }
        }
        public Scope CompileScope(Source src, AstScope s)
        {
            var vscope = new VariableScope();
            var scope  = new Scope(src);

            VariableScopeStack.Add(vscope);

            foreach (var e in s.Statements)
            {
                scope.Statements.Add(CompileStatement(e));
            }

            if (s.IsClosed)
            {
                VariableScopeStack.Remove(vscope);
                CurrentVariableScope.Scopes.Add(vscope);
            }

            return(scope);
        }
Beispiel #15
0
 public void WriteScope(AstScope a)
 {
     Write(a.Source);
     WriteStatements(a.Statements);
 }
Beispiel #16
0
 public SetupScopeChainingAndHandleDefinitionsTreeWalker(ScopeOptions options, AstToplevel astToplevel)
 {
     _options      = options;
     _currentScope = astToplevel.ParentScope = null;
     _defun        = null;
 }
Beispiel #17
0
        protected override void Visit(AstNode node)
        {
            if (node is IMayBeBlockScope blockScope && blockScope.IsBlockScope)
            {
                var saveScope = _currentScope;
                blockScope.BlockScope = _currentScope = new AstScope(node);
                _currentScope.InitScopeVars(saveScope);
                if (!(node is AstScope))
                {
                    _currentScope.UsesWith = saveScope.UsesWith;
                    _currentScope.UsesEval = saveScope.UsesEval;
                    _currentScope.HasUseStrictDirective = saveScope.HasUseStrictDirective;
                }

                DescendOnce();
                _currentScope = saveScope;
                return;
            }

            if (node is AstDestructuring destructuring)
            {
                _inDestructuring = destructuring; // These don't nest
                DescendOnce();
                _inDestructuring = null;
                return;
            }

            if (node is AstScope astScope)
            {
                astScope.InitScopeVars(_currentScope);
                var saveScope  = _currentScope;
                var saveDefun  = _defun;
                var saveLabels = _labels;
                _labels = new Dictionary <string, AstLabel>();
                _defun  = _currentScope = astScope;
                DescendOnce();
                _defun        = saveDefun;
                _currentScope = saveScope;
                _labels       = saveLabels;
                return;
            }

            if (node is AstLabeledStatement labeledStatement)
            {
                var l = labeledStatement.Label;
                if (!_labels.TryAdd(l.Name, l))
                {
                    throw new Exception($"Label {l.Name} defined twice");
                }

                DescendOnce();
                _labels.Remove(l.Name);
                return;
            }

            if (node is AstWith)
            {
                for (var s = _currentScope; s != null; s = s.ParentScope)
                {
                    s.UsesWith = true;
                }

                return;
            }

            if (node is AstSymbol astSymbol)
            {
                astSymbol.Scope = _currentScope;
            }

            if (node is AstLabel astLabel)
            {
                astLabel.References = new StructList <AstLoopControl>();
            }
            else if (node is AstSymbolLambda astSymbolLambda)
            {
                _defun.DefFunction(astSymbolLambda, astSymbolLambda.Name == "arguments" ? null : _defun);
            }
            else if (node is AstSymbolDefun astSymbolDefun)
            {
                // This should be defined in the parent scope, as we encounter the
                // AstDefun node before getting to its AstSymbol.
                MarkExport((astSymbolDefun.Scope = _defun.ParentScope.Resolve()).DefFunction(astSymbolDefun, _defun),
                           1);
            }
            else if (node is AstSymbolClass)
            {
                MarkExport(_defun.DefVariable((AstSymbol)node, _defun), 1);
            }
            else if (node is AstSymbolImport)
            {
                _currentScope.DefVariable((AstSymbol)node, null);
            }
            else if (node is AstSymbolDefClass)
            {
                // This deals with the name of the class being available
                // inside the class.
                MarkExport((((AstSymbol)node).Scope = _defun.ParentScope).DefFunction((AstSymbol)node, _defun), 1);
            }
            else if (node is AstSymbolVar ||
                     node is AstSymbolLet ||
                     node is AstSymbolConst)
            {
                SymbolDef def;
                if (node is AstSymbolBlockDeclaration)
                {
                    def = _currentScope.DefVariable((AstSymbol)node, null);
                }
                else
                {
                    def = _defun.DefVariable((AstSymbol)node, null);
                }

                if (!def.Orig.All(sym =>
                {
                    if (sym == node)
                    {
                        return(true);
                    }
                    if (node is AstSymbolBlockDeclaration)
                    {
                        return(sym is AstSymbolLambda);
                    }

                    return(!(sym is AstSymbolLet || sym is AstSymbolConst));
                }))
                {
                    throw new Exception(((AstSymbol)node).Name + " redeclared");
                }

                MarkExport(def, 2);
                def.Destructuring = _inDestructuring;
                if (_defun != _currentScope)
                {
                    ((AstSymbol)node).MarkEnclosed(_options);
                    var def2 = _currentScope.FindVariable((AstSymbol)node);
                    if (((AstSymbol)node).Thedef != def2)
                    {
                        ((AstSymbol)node).Thedef = def2;
                        ((AstSymbol)node).Reference(_options);
                    }
                }
            }
            else if (node is AstSymbolCatch astSymbolCatch)
            {
                _currentScope.DefVariable(astSymbolCatch, null).Defun = _defun;
            }
            else if (node is AstLabelRef labelRef)
            {
                if (_labels.TryGetValue(labelRef.Name, out var sym))
                {
                    labelRef.Thedef = sym;
                }
                else
                {
                    throw new Exception(
                              $"Undefined label {labelRef.Name} [{labelRef.Start.Line},{labelRef.Start.Column}]");
                }
            }

            if (!(_currentScope is AstToplevel) && (node is AstExport || node is AstImport))
            {
                throw new Exception(node.PrintToString() + " statement may only appear at top level");
            }
        }
 public Scope CompileScope(AstScope scope)
 {
     return(CompileScope(scope.Source, scope));
 }