Exemplo n.º 1
0
        public Value Invoke(Value objectVar, List<Value> parameters)
        {
            if (parameters.Count == parameterNames.Count)
            {
                // create new scope for the method call
                var methodScope = new Scope();
                methodScope.Define("this");
                methodScope.Assign("this", objectVar);
                for (int i = 0; i < parameterNames.Count; i++)
                {
                    methodScope.Define(parameterNames[i]);
                    methodScope.Assign(parameterNames[i], parameters[i]);
                }


                var nodeStream = new CommonTreeNodeStream(functionBody);
                // Create a tree walker to evaluate this method's code block  
                var walker = new SGLTreeWalker(nodeStream, methodScope);

                Value returnValue = null;

                // Ok, executing the function then
                returnValue = walker.main().Evaluate();


                // we shouldn't check the return type
                /*if (!returnValue.GetVarType().Equals(this.returnType))
                {
                    throw new Exception("The method doesn't return the expected return type (" + returnValue.ToString()  + " is not from type " + this.returnType + ")");
                }*/
                return returnValue;
			}

			throw new CompilerException(definedLine, 318, name, parameterNames.Count.ToString(), parameters.Count.ToString());
        }
Exemplo n.º 2
0
 // global assign
 public AssignVariableNode(String name, List<AbstractNode> indexes, AbstractNode expression, int line)
 {
     this.name = name;
     this.indexes = (indexes == null) ? new List<AbstractNode>() : indexes;
     this.expression = expression;
     scope = GlobalMemory.Instance.GlobalScope;
     this.line = line;
 }
Exemplo n.º 3
0
 public AssignVariableNode(String name, List<AbstractNode> indexes, AbstractNode expression, Scope scope,
                           int line)
 {
     this.name = name;
     this.indexes = (indexes == null) ? new List<AbstractNode>() : indexes;
     this.expression = expression;
     this.scope = scope;
     this.line = line;
 }
Exemplo n.º 4
0
 public InvokeFunctionNode(String objectVar, List<AbstractNode> indexes, String funcName,
                           List<AbstractNode> parameters, Scope scope, int line)
 {
     this.funcName = funcName;
     this.parameters = parameters != null ? parameters : new List<AbstractNode>();
     this.objectVar = objectVar;
     this.scope = scope;
     this.line = line;
 }
Exemplo n.º 5
0
        public override Value Invoke(List<Value> parameters)
        {
            if (parameters.Count == parameterNames.Count)
            {
                // create new scope for the method call
                var methodScope = new Scope();
                for (int i = 0; i < parameterNames.Count; i++)
                {
                    methodScope.Define(parameterNames[i]);
                    methodScope.Assign(parameterNames[i], parameters[i]);
                }

                var nodeStream = new CommonTreeNodeStream(functionBody);
                // Create a tree walker to evaluate this method's code block
                var walker = new SGLTreeWalker(nodeStream, methodScope);

                // Ok, executing the function then
                Value returnValue = walker.main().Evaluate();
                if (returnValue.Type == ValType.Return)
                {
                    return returnValue.ReturnValue;
                }
                else
                {
                    return returnValue;
                }

                // we shouldn't check the return type
                /*if (!returnValue.GetVarType().Equals(this.returnType))
                {
                    throw new Exception("The method doesn't return the expected return type (" + returnValue.ToString()  + " is not from type " + this.returnType + ")");
                }*/

            }
            // TODO: Exception
            throw new Exception();
        }
Exemplo n.º 6
0
 // global assign
 public DefineVariableNode(String name, int line)
 {
     this.name = name;
     scope = GlobalMemory.Instance.GlobalScope;
     this.line = line;
 }
Exemplo n.º 7
0
 public DefineVariableNode(String name, Scope scope, int line)
 {
     this.name = name;
     this.scope = scope;
     this.line = line;
 }
Exemplo n.º 8
0
 public Scope(Scope p)
 {
     parent = p;
     variables = new Dictionary<String, Value>();
     offset = p.Offset;
 }
Exemplo n.º 9
0
        private Dictionary<String, Value> variables; // = new Dictionary<String, SGLValue>();

        public Scope()
        {
            // only for the main scope, the parent is null  
            parent = null;
            variables = new Dictionary<String, Value>();
        }
Exemplo n.º 10
0
 public BlockNode(Scope scope)
 {
     statements = new List<AbstractNode>();
     this.scope = scope;
 }
Exemplo n.º 11
0
    // $ANTLR end "main"


    // $ANTLR start "block"
    // E:\\SGL\\sgl4c#\\NewSGLGramma\\SGLTreeWalker.g:136:1: block returns [AbstractNode node] : ^( BLOCK ( statement )* ) ;
    public AbstractNode block() // throws RecognitionException [1]
    {   
        AbstractNode node = default(AbstractNode);

        AbstractNode statement2 = default(AbstractNode);


         
          Scope scope = new Scope(currentScope); 
          currentScope = scope;
          BlockNode bn = new BlockNode(currentScope); 
          node = bn;  

        try 
    	{
            // E:\\SGL\\sgl4c#\\NewSGLGramma\\SGLTreeWalker.g:146:2: ( ^( BLOCK ( statement )* ) )
            // E:\\SGL\\sgl4c#\\NewSGLGramma\\SGLTreeWalker.g:146:4: ^( BLOCK ( statement )* )
            {
            	Match(input,BLOCK,FOLLOW_BLOCK_in_block104); 

            	if ( input.LA(1) == Token.DOWN )
            	{
            	    Match(input, Token.DOWN, null); 
            	    // E:\\SGL\\sgl4c#\\NewSGLGramma\\SGLTreeWalker.g:147:10: ( statement )*
            	    do 
            	    {
            	        int alt1 = 2;
            	        int LA1_0 = input.LA(1);

            	        if ( ((LA1_0 >= DEF && LA1_0 <= ASSIGN) || (LA1_0 >= VARINC && LA1_0 <= VARDEC) || (LA1_0 >= FUNC_CALL && LA1_0 <= IF) || (LA1_0 >= 84 && LA1_0 <= 85)) )
            	        {
            	            alt1 = 1;
            	        }


            	        switch (alt1) 
            	    	{
            	    		case 1 :
            	    		    // E:\\SGL\\sgl4c#\\NewSGLGramma\\SGLTreeWalker.g:147:11: statement
            	    		    {
            	    		    	PushFollow(FOLLOW_statement_in_block119);
            	    		    	statement2 = statement();
            	    		    	state.followingStackPointer--;

            	    		    	bn.AddStatement(statement2);

            	    		    }
            	    		    break;

            	    		default:
            	    		    goto loop1;
            	        }
            	    } while (true);

            	    loop1:
            	    	;	// Stops C# compiler whining that label 'loop1' has no statements


            	    Match(input, Token.UP, null); 
            	}

            }

             
              currentScope = currentScope.Parent; 

        }
        catch (RecognitionException re) 
    	{
            ReportError(re);
            Recover(input,re);
        }
        finally 
    	{
        }
        return node;
    }
Exemplo n.º 12
0
 public SGLTreeWalker(CommonTreeNodeStream nodes, Scope sc) : this(nodes) {
 	this.currentScope = sc;
 }
Exemplo n.º 13
0
 	public SGLTreeWalker(CommonTreeNodeStream nodes, Boolean isMain) : this(nodes) {
     	this.currentScope = new Scope();
     	this.isMain = isMain;
     }
Exemplo n.º 14
0
    // $ANTLR end "ifStatement"


    // $ANTLR start "forLoop"
    // E:\\SGL\\sgl4c#\\NewSGLGramma\\SGLTreeWalker.g:205:1: forLoop returns [AbstractNode node] : ^( 'for' ^( FORDEC (dec= statement )* ) ^( FORCOND (cond= expression )? ) ^( FORITER (iter= statement )? ) block ) ;
    public AbstractNode forLoop() // throws RecognitionException [1]
    {   
        AbstractNode node = default(AbstractNode);

        AbstractNode dec = default(AbstractNode);

        AbstractNode cond = default(AbstractNode);

        AbstractNode iter = default(AbstractNode);

        AbstractNode block27 = default(AbstractNode);



          // We have to use 2 Blocks for the for-Node to work correctly
          // Create new block for the beginning of the for-Loop
          Scope scope = new Scope(currentScope); 
          currentScope = scope;
          BlockNode bn = new BlockNode(currentScope); 
          
          //Create the For-Node and add it to the block
          ForNode forNode = new ForNode();
          bn.AddStatement(forNode);
          
          // Return the block node
          node = bn;

        try 
    	{
            // E:\\SGL\\sgl4c#\\NewSGLGramma\\SGLTreeWalker.g:223:2: ( ^( 'for' ^( FORDEC (dec= statement )* ) ^( FORCOND (cond= expression )? ) ^( FORITER (iter= statement )? ) block ) )
            // E:\\SGL\\sgl4c#\\NewSGLGramma\\SGLTreeWalker.g:223:4: ^( 'for' ^( FORDEC (dec= statement )* ) ^( FORCOND (cond= expression )? ) ^( FORITER (iter= statement )? ) block )
            {
            	Match(input,85,FOLLOW_85_in_forLoop517); 

            	Match(input, Token.DOWN, null); 
            	Match(input,FORDEC,FOLLOW_FORDEC_in_forLoop520); 

            	if ( input.LA(1) == Token.DOWN )
            	{
            	    Match(input, Token.DOWN, null); 
            	    // E:\\SGL\\sgl4c#\\NewSGLGramma\\SGLTreeWalker.g:223:21: (dec= statement )*
            	    do 
            	    {
            	        int alt9 = 2;
            	        int LA9_0 = input.LA(1);

            	        if ( ((LA9_0 >= DEF && LA9_0 <= ASSIGN) || (LA9_0 >= VARINC && LA9_0 <= VARDEC) || (LA9_0 >= FUNC_CALL && LA9_0 <= IF) || (LA9_0 >= 84 && LA9_0 <= 85)) )
            	        {
            	            alt9 = 1;
            	        }


            	        switch (alt9) 
            	    	{
            	    		case 1 :
            	    		    // E:\\SGL\\sgl4c#\\NewSGLGramma\\SGLTreeWalker.g:223:22: dec= statement
            	    		    {
            	    		    	PushFollow(FOLLOW_statement_in_forLoop525);
            	    		    	dec = statement();
            	    		    	state.followingStackPointer--;

            	    		    	 forNode.Init = dec; 

            	    		    }
            	    		    break;

            	    		default:
            	    		    goto loop9;
            	        }
            	    } while (true);

            	    loop9:
            	    	;	// Stops C# compiler whining that label 'loop9' has no statements


            	    Match(input, Token.UP, null); 
            	}
            	Match(input,FORCOND,FOLLOW_FORCOND_in_forLoop534); 

            	if ( input.LA(1) == Token.DOWN )
            	{
            	    Match(input, Token.DOWN, null); 
            	    // E:\\SGL\\sgl4c#\\NewSGLGramma\\SGLTreeWalker.g:223:80: (cond= expression )?
            	    int alt10 = 2;
            	    int LA10_0 = input.LA(1);

            	    if ( ((LA10_0 >= STRING && LA10_0 <= VARDEC) || LA10_0 == LOOKUP || LA10_0 == CLASS || (LA10_0 >= IntAtom && LA10_0 <= LoopTriggerAtom) || LA10_0 == Null || LA10_0 == 62 || (LA10_0 >= 64 && LA10_0 <= 77)) )
            	    {
            	        alt10 = 1;
            	    }
            	    switch (alt10) 
            	    {
            	        case 1 :
            	            // E:\\SGL\\sgl4c#\\NewSGLGramma\\SGLTreeWalker.g:223:81: cond= expression
            	            {
            	            	PushFollow(FOLLOW_expression_in_forLoop539);
            	            	cond = expression();
            	            	state.followingStackPointer--;

            	            	 forNode.Condition = cond; 

            	            }
            	            break;

            	    }


            	    Match(input, Token.UP, null); 
            	}
            	Match(input,FORITER,FOLLOW_FORITER_in_forLoop547); 

            	if ( input.LA(1) == Token.DOWN )
            	{
            	    Match(input, Token.DOWN, null); 
            	    // E:\\SGL\\sgl4c#\\NewSGLGramma\\SGLTreeWalker.g:223:146: (iter= statement )?
            	    int alt11 = 2;
            	    int LA11_0 = input.LA(1);

            	    if ( ((LA11_0 >= DEF && LA11_0 <= ASSIGN) || (LA11_0 >= VARINC && LA11_0 <= VARDEC) || (LA11_0 >= FUNC_CALL && LA11_0 <= IF) || (LA11_0 >= 84 && LA11_0 <= 85)) )
            	    {
            	        alt11 = 1;
            	    }
            	    switch (alt11) 
            	    {
            	        case 1 :
            	            // E:\\SGL\\sgl4c#\\NewSGLGramma\\SGLTreeWalker.g:223:147: iter= statement
            	            {
            	            	PushFollow(FOLLOW_statement_in_forLoop552);
            	            	iter = statement();
            	            	state.followingStackPointer--;

            	            	 forNode.Iteration = iter; 

            	            }
            	            break;

            	    }


            	    Match(input, Token.UP, null); 
            	}
            	PushFollow(FOLLOW_block_in_forLoop559);
            	block27 = block();
            	state.followingStackPointer--;


            	Match(input, Token.UP, null); 
            	 forNode.Block = block27; 

            }

             
              currentScope = currentScope.Parent; 

        }
        catch (RecognitionException re) 
    	{
            ReportError(re);
            Recover(input,re);
        }
        finally 
    	{
        }
        return node;
    }
Exemplo n.º 15
0
 public VarDecNode(String identifier, Scope scope, int line)
 {
     this.identifier = identifier;
     this.scope = scope;
     this.line = line;
 }