コード例 #1
0
ファイル: Interpreter.cs プロジェクト: tony-karandeev/SimpleC
        public void Prepare(RootNode tree)
        {
            funcDefinitions_.Clear();

            foreach (ISCNode node in tree.Children)
            {
                if (node is VarDefNode)
                {
                    globalScope_.Add(node as VarDefNode);
                }
                else if (node is FuncDefNode)
                {
                    FuncDefNode funcDef = node as FuncDefNode;
                    if (funcDefinitions_.Contains(funcDef))
                        throw new InterpreterException("Multiple function definitions for function: " + funcDef.Name);
                    funcDefinitions_.Add(funcDef);

                    if (funcDef.Name == "main")
                        mainFuncDef_ = funcDef;
                }
                else if (node is FuncDeclNode)
                {
                    // Just ignore function declarations
                }
            }
            if (mainFuncDef_ == null)
                throw new InterpreterException("Missing 'main' function");
            globalScope_.FuncDefs = funcDefinitions_;
        }
コード例 #2
0
 public bool Contains(FuncDefNode funcDef)
 {
     foreach (FuncDefNode def in definitions_)
         if (def.Equatable.Equals(funcDef.Equatable))
             return true;
     return false;
 }
コード例 #3
0
	private FuncDefNode funcDef()
	{
		EnterRule_funcDef();
		EnterRule("funcDef", 5);
		TraceIn("funcDef", 5);
		FuncDefNode node = default(FuncDefNode);


		CommonTree IDENT10 = default(CommonTree);
		TypeSpecNode typeSpec9 = default(TypeSpecNode);
		VarSpecNode varSpec11 = default(VarSpecNode);
		CompoundStatementNode funcBody12 = default(CompoundStatementNode);


			node = new FuncDefNode();

		try { DebugEnterRule(GrammarFileName, "funcDef");
		DebugLocation(54, 0);
		try
		{
			// Grammar\\SimpleCTreeWalker.g:57:3: ( ^( FUNC_DEF typeSpec IDENT ^( PARAM_SPEC_LIST ( varSpec )* ) funcBody ) )
			DebugEnterAlt(1);
			// Grammar\\SimpleCTreeWalker.g:58:2: ^( FUNC_DEF typeSpec IDENT ^( PARAM_SPEC_LIST ( varSpec )* ) funcBody )
			{
			DebugLocation(58, 2);
			DebugLocation(58, 4);
			Match(input,FUNC_DEF,Follow._FUNC_DEF_in_funcDef188); 

			Match(input, TokenTypes.Down, null); 
			DebugLocation(59, 3);
			PushFollow(Follow._typeSpec_in_funcDef193);
			typeSpec9=typeSpec();
			PopFollow();

			DebugLocation(59, 12);
			 node.ReturnTypeSpec = typeSpec9; 
			DebugLocation(60, 3);
			IDENT10=(CommonTree)Match(input,IDENT,Follow._IDENT_in_funcDef199); 
			DebugLocation(60, 9);
			 node.Name = IDENT10.Text; 
			DebugLocation(61, 3);
			DebugLocation(61, 5);
			Match(input,PARAM_SPEC_LIST,Follow._PARAM_SPEC_LIST_in_funcDef206); 

			if (input.LA(1) == TokenTypes.Down)
			{
				Match(input, TokenTypes.Down, null); 
				DebugLocation(61, 21);
				// Grammar\\SimpleCTreeWalker.g:61:21: ( varSpec )*
				try { DebugEnterSubRule(4);
				while (true)
				{
					int alt4=2;
					try { DebugEnterDecision(4, false);
					int LA4_0 = input.LA(1);

					if ((LA4_0==VAR_SPEC))
					{
						alt4 = 1;
					}


					} finally { DebugExitDecision(4); }
					switch ( alt4 )
					{
					case 1:
						DebugEnterAlt(1);
						// Grammar\\SimpleCTreeWalker.g:61:22: varSpec
						{
						DebugLocation(61, 22);
						PushFollow(Follow._varSpec_in_funcDef209);
						varSpec11=varSpec();
						PopFollow();

						DebugLocation(61, 30);
						 node.Parameters.Add(varSpec11); 

						}
						break;

					default:
						goto loop4;
					}
				}

				loop4:
					;

				} finally { DebugExitSubRule(4); }


				Match(input, TokenTypes.Up, null); 
			}

			DebugLocation(62, 3);
			PushFollow(Follow._funcBody_in_funcDef219);
			funcBody12=funcBody();
			PopFollow();

			DebugLocation(62, 12);
			 node.Body = funcBody12; 

			Match(input, TokenTypes.Up, null); 


			}

		}
		catch (RecognitionException re)
		{
			ReportError(re);
			Recover(input,re);
		}
		finally
		{
			TraceOut("funcDef", 5);
			LeaveRule("funcDef", 5);
			LeaveRule_funcDef();
		}
		DebugLocation(63, 0);
		} finally { DebugExitRule(GrammarFileName, "funcDef"); }
		return node;

	}
コード例 #4
0
 public void Add(FuncDefNode funcDef)
 {
     definitions_.Add(funcDef);
 }
コード例 #5
0
	private FuncDefNode funcDef()
	{
		EnterRule_funcDef();
		EnterRule("funcDef", 6);
		TraceIn("funcDef", 6);
		FuncDefNode node = default(FuncDefNode);


		CommonTree IDENT13 = default(CommonTree);
		TypeSpecNode typeSpec12 = default(TypeSpecNode);
		VarSpecNode varSpec14 = default(VarSpecNode);
		CompoundStatementNode funcBody15 = default(CompoundStatementNode);


			node = new FuncDefNode();

		try { DebugEnterRule(GrammarFileName, "funcDef");
		DebugLocation(71, 0);
		try
		{
			// Grammar\\SimpleCTreeWalker.g:74:3: ( ^( FUNC_DEF typeSpec IDENT ^( PARAM_SPEC_LIST ( varSpec )* ) funcBody ) )
			DebugEnterAlt(1);
			// Grammar\\SimpleCTreeWalker.g:75:2: ^( FUNC_DEF typeSpec IDENT ^( PARAM_SPEC_LIST ( varSpec )* ) funcBody )
			{
			DebugLocation(75, 2);
			DebugLocation(75, 4);
			Match(input,FUNC_DEF,Follow._FUNC_DEF_in_funcDef241); 

			Match(input, TokenTypes.Down, null); 
			DebugLocation(76, 3);
			PushFollow(Follow._typeSpec_in_funcDef246);
			typeSpec12=typeSpec();
			PopFollow();

			DebugLocation(76, 12);
			 node.ReturnTypeSpec = typeSpec12; 
			DebugLocation(77, 3);
			IDENT13=(CommonTree)Match(input,IDENT,Follow._IDENT_in_funcDef252); 
			DebugLocation(77, 9);
			 node.Name = IDENT13.Text; 
			DebugLocation(78, 3);
			DebugLocation(78, 5);
			Match(input,PARAM_SPEC_LIST,Follow._PARAM_SPEC_LIST_in_funcDef259); 

			if (input.LA(1) == TokenTypes.Down)
			{
				Match(input, TokenTypes.Down, null); 
				DebugLocation(78, 21);
				// Grammar\\SimpleCTreeWalker.g:78:21: ( varSpec )*
				try { DebugEnterSubRule(5);
				while (true)
				{
					int alt5=2;
					try { DebugEnterDecision(5, false);
					int LA5_0 = input.LA(1);

					if ((LA5_0==VAR_SPEC))
					{
						alt5 = 1;
					}


					} finally { DebugExitDecision(5); }
					switch ( alt5 )
					{
					case 1:
						DebugEnterAlt(1);
						// Grammar\\SimpleCTreeWalker.g:78:22: varSpec
						{
						DebugLocation(78, 22);
						PushFollow(Follow._varSpec_in_funcDef262);
						varSpec14=varSpec();
						PopFollow();

						DebugLocation(78, 30);
						 node.Parameters.Add(varSpec14); 

						}
						break;

					default:
						goto loop5;
					}
				}

				loop5:
					;

				} finally { DebugExitSubRule(5); }


				Match(input, TokenTypes.Up, null); 
			}

			DebugLocation(79, 3);
			PushFollow(Follow._funcBody_in_funcDef272);
			funcBody15=funcBody();
			PopFollow();

			DebugLocation(79, 12);
			 node.Body = funcBody15; 

			Match(input, TokenTypes.Up, null); 


			}

		}
		catch (RecognitionException re)
		{
			ReportError(re);
			Recover(input,re);
		}
		finally
		{
			TraceOut("funcDef", 6);
			LeaveRule("funcDef", 6);
			LeaveRule_funcDef();
		}
		DebugLocation(80, 0);
		} finally { DebugExitRule(GrammarFileName, "funcDef"); }
		return node;

	}