Beispiel #1
0
        public void VisitSymbol(AstSymbol ast)
        {
            Assert.IsType <AstSymbol>(this.expected);
            var expectedAst = (AstSymbol)this.expected;

            Assert.Equal(expectedAst.Name, ast.Name);
        }
Beispiel #2
0
    public override void CodeGen(OutputContext output)
    {
        string GetName(AstSymbol symbol)
        {
            return(symbol.Thedef?.MangledName ?? symbol.Thedef?.Name ?? symbol.Name);
        }

        var allowShortHand = output.Options.Ecma >= 6;
        var keyString      = Key switch
        {
            AstString str => str.Value,
            AstNumber num => num.Value.ToString("R", CultureInfo.InvariantCulture),
            AstSymbolRef _ => null,
            AstSymbol key => GetName(key),
            _ => null
        };

        if (allowShortHand &&
            Value is AstSymbol value && keyString != null &&
            GetName(value) == keyString &&
            OutputContext.IsIdentifierString(keyString) &&
            OutputContext.IsIdentifier(keyString)
            )
        {
            output.PrintPropertyName(keyString);
        }
Beispiel #3
0
    public SymbolDef DefVariable(AstSymbol symbol, AstNode?init)
    {
        SymbolDef def;

        if (Variables !.ContainsKey(symbol.Name))
        {
            def = Variables ![symbol.Name];
Beispiel #4
0
    public SymbolDef DefGlobal(AstSymbol symbol)
    {
        var name = symbol.Name;

        if (Globals !.ContainsKey(name))
        {
            return(Globals ![name]);
Beispiel #5
0
        AstNode ParseLabelledStatement(Position nodeStart, string maybeName, AstSymbol expr)
        {
            foreach (var label in _labels)
            {
                if (label.Name == maybeName)
                {
                    Raise(expr.Start, "Label '" + maybeName + "' is already declared");
                }
            }

            var newlabel = new AstLabel(this, nodeStart, _lastTokEnd, maybeName);

            newlabel.IsLoop = TokenInformation.Types[Type].IsLoop;
            _labels.Add(newlabel);
            var body = ParseStatement(true);

            if (body is AstClass ||
                body is AstLet || body is AstConst ||
                body is AstFunction functionDeclaration && (_strict || functionDeclaration.IsGenerator))
            {
                RaiseRecoverable(body.Start, "Invalid labelled declaration");
            }
            _labels.Pop();

            return(new AstLabeledStatement(this, nodeStart, _lastTokEnd, (AstStatement)body, newlabel));
        }
Beispiel #6
0
 public static void SetSymbol(this IAstSymbolSite symbolEntrySite, AstSymbol symbolEntry)
 {
     if (!symbolEntrySite.TrySetSymbol(symbolEntry))
     {
         throw new InternalErrorException(
                   "Symbol is already set or null.");
     }
 }
Beispiel #7
0
        internal void AstSymbol_Clone_ClonesTheObject()
        {
            var ast    = new AstSymbol("foobar");
            var cloned = ast.Clone();

            Assert.NotSame(ast, cloned);
            Assert.Equal(ast, cloned);
            Assert.Equal(ast.Name, ((AstSymbol)cloned).Name);
        }
        protected override string GetSymbolTargetName(AstSymbol symbol)
        {
            if (symbol.IsValidFrame)
            {
                return(UniqueNameGenerator.GetUniqueName(symbol.Name));
            }

            throw new InvalidOperationException();
        }
Beispiel #9
0
        public static AstExpression Create(Source src, params string[] qualifier)
        {
            AstExpression @base = new AstSymbol(src, AstSymbolType.Global);

            foreach (var id in qualifier)
            {
                @base = new AstMember(@base,
                                      new AstIdentifier(src, id));
            }
            return(@base);
        }
Beispiel #10
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 #11
0
        public void Eval_Defines_ValueReferenced()
        {
            var env   = new LispEnv();
            var name  = new AstSymbol("x");
            var value = new AstIntNumber(42);

            env.Eval(new AstList(
                         new AstSymbol("def!"),
                         name,
                         value));
            var data = env.Copy();

            Assert.Equal(value, data.Defines[name]);
        }
Beispiel #12
0
        /// <summary>
        /// Close the child scope of the given namespace or frame
        /// </summary>
        /// <param name="symbol"></param>
        public void CloseScope(AstSymbol symbol)
        {
            if (symbol.IsValidNamespace)
            {
                CloseScope((AstNamespace)symbol);
                return;
            }

            if (symbol.IsValidFrame)
            {
                CloseScope((AstFrame)symbol);
                return;
            }

            this.ReportNormal("Close Scope", ProgressEventArgsResult.Failure);
        }
Beispiel #13
0
        public void Clone_Defines_HasNotSameObject_ButEqual()
        {
            var data        = new LispEnvData();
            var sourceKey   = new AstSymbol("foo");
            var sourceValue = new AstSymbol("bar");

            data.Defines[sourceKey] = sourceValue;

            var dataClone  = data.Clone();
            var keyClone   = dataClone.Defines.Keys.Single();
            var valueClone = (AstSymbol)dataClone.Defines[sourceKey];

            Assert.NotSame(sourceKey, keyClone);
            Assert.NotSame(sourceValue, valueClone);

            Assert.Equal(sourceKey.Name, keyClone.Name);
            Assert.Equal(sourceValue.Name, valueClone.Name);
        }
Beispiel #14
0
        /// <summary>
        /// The main Reset method called by all others. The given symbol must be a frame or a namespace
        /// </summary>
        /// <param name="symbol"></param>
        public void Reset(AstSymbol symbol)
        {
            if (symbol.IsNullOrInvalid() || (symbol.IsValidFrame == false && symbol.IsValidNamespace == false))
            {
                this.ReportNormal("Reset Interpreter", ProgressEventArgsResult.Failure);

                return;
            }

            Output.Clear();

            Root = symbol.Root;

            _mainCommandBlock = CommandBlock.Create(symbol.AssociatedSymbol as SymbolWithScope);

            RefResContext.Clear(_mainCommandBlock);

            _expressionEvaluator = GMacExpressionEvaluator.CreateForDynamicEvaluation(_mainCommandBlock);

            _symbolsCache.Clear();

            this.ReportNormal("Reset Interpreter", symbol.AccessName, ProgressEventArgsResult.Success);
        }
Beispiel #15
0
    protected void PrintGetterSetter(OutputContext output, string?type, bool @static)
    {
        if (@static)
        {
            output.Print("static");
            output.Space();
        }

        if (type != null)
        {
            output.Print(type);
            output.Space();
        }

        var keyString = Key switch
        {
            AstString str => str.Value,
            AstNumber num => num.Value.ToString("R", CultureInfo.InvariantCulture),
            AstSymbolRef => null,
            AstSymbol key => key.Name,
            _ => null
        };

        if (keyString != null)
        {
            output.PrintPropertyName(keyString);
        }
        else
        {
            output.Print("[");
            Key.Print(output);
            output.Print("]");
        }

        ((AstLambda)Value).DoPrint(output, true);
    }
}
Beispiel #16
0
 public AstSymbolLambda(AstSymbol from) : base(from)
 {
 }
Beispiel #17
0
 protected override string GetSymbolTargetName(AstSymbol symbol)
 {
     return(symbol.AccessName);
 }
Beispiel #18
0
 public AstSymbolExport(AstSymbol symbol) : base(symbol)
 {
 }
Beispiel #19
0
 public AstSymbolVar(AstSymbol name) : base(name)
 {
 }
Beispiel #20
0
    static bool IsVarLetConst(AstSymbol astSymbol)
    {
        var t = astSymbol.GetType();

        return(t == typeof(AstSymbolVar) || t == typeof(AstSymbolLet) || t == typeof(AstSymbolConst));
    }
Beispiel #21
0
 public AstSymbolRef(AstSymbol symbol) : base(symbol)
 {
 }
Beispiel #22
0
 internal SimpleSelfExport(string name, AstSymbol symbol)
 {
     Name   = name;
     Symbol = symbol;
 }
Beispiel #23
0
 public AstSymbolDefun(AstSymbol from) : base(from)
 {
 }
Beispiel #24
0
        AstNode ParseFunction(Position startLoc, bool isStatement, bool isNullableId, bool allowExpressionBody = false,
                              bool isAsync = false)
        {
            var generator = false;

            if (Options.EcmaVersion >= 6 && !isAsync)
            {
                generator = Eat(TokenType.Star);
            }
            if (Options.EcmaVersion < 8 && isAsync)
            {
                throw new InvalidOperationException();
            }

            AstSymbol id = null;

            if (isStatement || isNullableId)
            {
                id = isNullableId && Type != TokenType.Name ? null : ParseIdent();
                if (id != null)
                {
                    CheckLVal(id, true, VariableKind.Var);
                }
            }

            var oldInGen    = _inGenerator;
            var oldInAsync  = _inAsync;
            var oldYieldPos = _yieldPos;
            var oldAwaitPos = _awaitPos;
            var oldInFunc   = _inFunction;

            _inGenerator = generator;
            _inAsync     = isAsync;
            _yieldPos    = default;
            _awaitPos    = default;
            _inFunction  = true;
            EnterFunctionScope();

            if (isStatement == false && isNullableId == false)
            {
                id = Type == TokenType.Name ? ParseIdent() : null;
            }

            var parameters = new StructList <AstNode>();

            ParseFunctionParams(ref parameters);
            MakeSymbolFunArg(ref parameters);
            var body       = new StructList <AstNode>();
            var useStrict  = false;
            var expression = ParseFunctionBody(parameters, startLoc, id, allowExpressionBody, ref body, ref useStrict);

            _inGenerator = oldInGen;
            _inAsync     = oldInAsync;
            _yieldPos    = oldYieldPos;
            _awaitPos    = oldAwaitPos;
            _inFunction  = oldInFunc;

            if (isStatement || isNullableId)
            {
                if (id != null)
                {
                    id = new AstSymbolDefun(id);
                }
                return(new AstDefun(this, startLoc, _lastTokEnd, (AstSymbolDefun)id, ref parameters, generator,
                                    isAsync, ref body).SetUseStrict(useStrict));
            }

            if (id != null)
            {
                id = new AstSymbolLambda(id);
            }
            return(new AstFunction(this, startLoc, _lastTokEnd, (AstSymbolLambda)id, ref parameters, generator,
                                   isAsync, ref body).SetUseStrict(useStrict));
        }
Beispiel #25
0
 public AstSymbolFunarg(AstSymbol from) : base(from)
 {
 }
Beispiel #26
0
 public AstSymbolCatch(AstSymbol name) : base(name)
 {
 }
Beispiel #27
0
 public AstSymbolDefClass(AstSymbol name, AstNode init = null) : base(name, init)
 {
 }
Beispiel #28
0
 public AstSymbolProperty(AstSymbol symbol) : base(symbol)
 {
 }
Beispiel #29
0
 public AstSymbolClass(AstSymbol from) : base(from)
 {
 }
Beispiel #30
0
 public void VisitSymbol(AstSymbol ast) => IncrementCallCount(nameof(VisitSymbol));