コード例 #1
0
        public Ast_Base ResolveStructPart(Token token, ParserState state)
        {
            var structPart = token.Clone();
            var sb         = new StringBuilder();

            sb.Append(token.Lexeme);
            token = state.PeekToken();
            while (!state.EOF && token.Type == TokenType.Dot)
            {
                sb.Append(token.Lexeme);
                _     = Expect(TokenType.Dot, state);
                token = Expect(TokenType.Identifier, state);
                sb.Append(token.Lexeme);
                token = state.PeekToken();
            }
            structPart.Lexeme = sb.ToString();
            if (token.Type == TokenType.OpAssignLambda)
            {
                return(new Ast_Lambda(structPart));
            }
            else if (token.Type == TokenType.BracketLeft)
            {
                return(new Ast_Call(structPart, Libraries));
            }
            var ast = new Ast_Variable(structPart);

            if (token.Type == TokenType.IndexLeft)
            {
                ast.Index = ParseIndex(state);
            }
            return(ast);
        }
コード例 #2
0
ファイル: LibrarySystem.cs プロジェクト: NMJCaspers/TSP
            public override dynamic ExecuteCall(Ast_Scope scope)
            {
                var argLst = Args[0].Value.Value;
                var sb     = new StringBuilder();

                foreach (var v in argLst)
                {
                    if (v is Ast_Variable)
                    {
                        Ast_Variable variable = v as Ast_Variable;
                        // ToDo: Perhaps verify if i need to get the var from the scope to actually check!
                        dynamic value;
                        if (variable.Index != null)
                        {
                            value = variable.DoGetValue(variable.Index, scope);
                        }
                        else
                        {
                            value = variable.DoGetValue();
                        }
                        sb.Append(value.ToString());
                    }
                    else
                    {
                        sb.Append(v.Value);
                    }
                    sb.Append(' ');
                }
                if (sb.Length > 1)
                {
                    sb.Remove(sb.Length - 1, 1);
                }
                Console.WriteLine(sb.ToString());
                return(false);
            }
コード例 #3
0
ファイル: LibrarySystem.cs プロジェクト: NMJCaspers/TSP
            public Ast_Console(Token token) : base(token)
            {
                var variable = new Ast_Variable(new Token {
                    Lexeme = "args"
                });

                variable.DoSetValue(Ast_Variable.NewParamsValue);
                Args.Add(variable);
            }
コード例 #4
0
        public override dynamic Execute(Ast_Scope scope)
        {
            if (Block.Count == 0)
            {
                return(false);                  // Gatekeeper, don't run if there are no instructions in the block.
            }
            dynamic   containerValue = GetContainerValue(scope);
            Ast_Scope childScope     = scope.CreateChild("foreach");

            childScope.InLoop = true;
            childScope.IsLoop = true;
            Ast_Variable it = new Ast_Variable(null)
            {
                Name = "it"
            };

            childScope.Variables.Add(it);
            if (containerValue is string)
            {
                ExecuteForeachLoop(containerValue, childScope, it);
            }
            else if (containerValue.Type == AstType.Variable && containerValue.Value.Type == ValueType.Record)
            {
                ExecuteForeachLoop(containerValue.Value.Value.Keys, childScope, it);
            }
            else if (containerValue.Type == AstType.Variable && containerValue.Value.Type == ValueType.Array)
            {
                ExecuteForeachLoop(containerValue.Value.Value, childScope, it);
            }
            else if (containerValue.Type == AstType.Variable && containerValue.Value.Type == ValueType.String)
            {
                ExecuteForeachLoop(containerValue.Value.Value, childScope, it);
            }
            else
            {
                throw new RuntimeError(Token, "Invalid container value type");
            }
            childScope.InLoop = false;
            childScope.Variables.Remove(it);
            return(false);
        }
コード例 #5
0
 private void ExecuteForeachLoop(dynamic container, Ast_Scope currentScope, Ast_Variable it)
 {
     foreach (var fo in container)
     {
         if (fo is VT_Any)
         {
             it.DoSetValue(fo.Value);
         }
         else
         {
             it.DoSetValue(fo);
         }
         foreach (var fi in Block)
         {
             var res = fi.Execute(currentScope);
             if (res is Ast_Terminate || res == true)
             {
                 break;
             }
         }
     }
 }
コード例 #6
0
 public Ast_Base ParseIdentifier(Token token, ParserState state)
 {
     if (Keywords.Contains(token.Lexeme))
     {
         return(new Ast_Keyword(token));
     }
     else if (BoolOperands.Contains(token.Lexeme))
     {
         return(new Ast_BoolOp(token));
     }
     else if (token.Lexeme == Ast_Variable.NilValue)
     {
         return(new Ast_Constant(token));
     }
     else if (state.PeekToken().Type == TokenType.OpAssignLambda)
     {
         return(new Ast_Lambda(token));
     }
     else if (state.PeekToken().Type == TokenType.BracketLeft)
     {
         return(new Ast_Call(token, Libraries));
     }
     else if (VariableTypes.Contains(token.Lexeme))
     {
         return(new Ast_Type(token));
     }
     else if (state.PeekToken().Type == TokenType.Dot)
     {
         return(ResolveStructPart(token, state));
     }
     else // assume variable.
     {
         Ast_Variable ast;
         if (token.Type == TokenType.Ref)
         {
             if (state.Struct)
             {
                 throw new SyntaxError(token, "Struct definition cannot have ref!");
             }
             token = Expect(TokenType.Identifier, state);
             ast   = new Ast_Variable(token)
             {
                 Ref = true
             };
         }
         else
         {
             ast = new Ast_Variable(token);
         }
         var peek = state.PeekToken();
         if (peek.Type == TokenType.IndexLeft)
         {
             if (state.Struct)
             {
                 throw new SyntaxError(token, "Struct definition cannot have indexed var assignment!");
             }
             ast.Index = ParseIndex(state);
         }
         return(ast);
     }
 }