Commits

bookaa  committed c440871

701

  • Participants
  • Parent commits 70451ab

Comments (0)

Files changed (3)

             return 2
         return 'unknown'
 
-the = C()
-print the.a - (5-2), the.b + 3 * 4, the.c + 's'
+if __name__ == '__main__':
+    the = C()
+    print the.a - (5-2), the.b + 3 * 4, the.c + 's'
 '''
 
-mb = MainBrain.MainBrain()
-MainBrain.g_mb = mb
-mb.DefineInLang(LangDef.python)
-mb.ReadInBuf(src)
-for s in mb.Output():
-    print s
-print '-----------------'
-mb.DefineOutLang(LangDef.python_without_dyna)
-for s in mb.OutputCpp():
-    print s
-print '-----------------'
-mb.Run()
+if __name__ == '__main__':
+    mb = MainBrain.MainBrain()
+    MainBrain.g_mb = mb
+    mb.DefineInLang(LangDef.python)
+    mb.ReadInBuf(src)
+    for s in mb.Output():
+        print s
+    print '-----------------'
+    mb.DefineOutLang(LangDef.python_without_dyna)
+    for s in mb.OutputCpp():
+        print s
+    print '-----------------'
+    mb.Run()

File MainBrain.py

 from LangParse import LangParse
 import NameSpace
 import RunState
+import VarDefine
 
 class MainBrain():
     def __init__(self):
             yield s
 
     def OutputCpp(self):
-        for s in self.globalns.Output():
+        for s in self.globalns.OutputCpp():
             yield s
 
     def Run(self):
         runstate = RunState.RunState(self.globalns, None)
+
+        value = VarDefine.Value('string', '__main__')
+        var = VarDefine.NameToken('__name__')
+        runstate.SetVarValue(var, value)
+
         self.globalns.Run(runstate)
 

File NameSpace.py

             for s in op.Output():
                 yield s
 
+    def OutputCpp(self):
+        for s in self.OutputCpp_H():
+            yield s
+        yield '//-------------'
+        for s in self.OutputCpp_Cpp():
+            yield s
+
+    def OutputCpp_H(self):
+        yield '// head file -------------'
+        runstate = RunState.RunState(self, None)
+
+        value = VarDefine.Value('string', 'UserCpp')
+        var = VarDefine.NameToken('__name__')
+        runstate.SetVarValue(var, value)
+
+        self.Run(runstate)
+        pass
+
+
+    def OutputCpp_Cpp(self):
+        yield '// cpp file -------------'
+
     def Run(self, rs):
         for op in self.oplist:
             result = op.Run(rs)
             thefunc = initfunc[0]
             assert thefunc.__class__ is NameSpace.FuncNS
             assert len(thefunc.params) > 0 # the first should be 'self'
-            assert len(thefunc.params) == len(params) +1 # +1 is self, not support default param
+            assert len(thefunc.params) == len(params) + 1 # +1 is self, not support default param
             funcInstance = RunState.FuncInstance(thefunc, thefunc.params, classInstance)
             wordself = thefunc.params[0]
             funcInstance.DeclareParameter(wordself, value)
             for i in range(len(params)):
                 wd = params[i]
                 v = rs.GetVarRunState(wd)
-                funcInstance.DeclareParameter(thefunc.params[i+1], v)
+                funcInstance.DeclareParameter(thefunc.params[i + 1], v)
             thefunc.Run(funcInstance)
         else:
             assert not params #no param, it must be no __init__