Gigih Aji Ibrahim avatar Gigih Aji Ibrahim committed c026f7a

update exception

Comments (0)

Files changed (6)

 module std.exc
 
-@tgc__.Exception class Exception:pass
+@rt__ class Error(message Str)
 module std.io
 
+import std.exc
+
 interface ToStr:
     def toStr->Str
 
 @external("rt.builtins.print")    def print(s ToStr)
 @external("rt.builtins.printLn")  def printLn(s ToStr)
 
-enum Mode:
-    Read
-    Write
-    ReadWrite
+
+enum Mode: Read, Write, ReadWrite, Append, ReadAppend
+enum Type: Text, Binary
+
+//Suatu Saat Pakai Ini....
+//trait Print:
+    //def printTo(tgt OutStream) -> Bool
+
+//implicit strToPrint (value Str) = Print: it.write value
+//implicit intToPrint (value Int) = Print: it.write value
+/*implicit optionToPrint!(T < Print) (value Option!T) = Print:
+    value match:
+        case Some(data): data.writeTo data
+        case None: it.write "null"*/
+        
+
+//@rt__ def print(s Print)
+//@rt__ def printLn(s Print)
+
+//trait Writer:
+    //def write (data Byte{} ) -> Int
+
+//trait Reader:
+    //def read  (buffer Byte[] ) -> Int
+
+
+@rt__ class Error(message Str) < exc.Error(message)
+
 
 @rt__
-class File(filename Str):
-    def open(mode Mode)      -> Bool
-    def write(data Str)
-    def writeLn(data Str)
+class File(fileName Str):
+    def openText(mode Mode) -> Bool
+    def openBinary(mode Mode) -> Bool
     def close
 
+    /// properties
+    //def setMode(value Mode): &mode = value
+    //def mode->Mode: &mode
 
-def open(filename Str, mode Mode) -> File:
-    val handle = File(filename)
-    handle.open(mode)
+    /// Basic read/write
+    def write (data   List!Byte ) -> Int
+    def read  (buffer Array!Byte ) -> Int
+
+    /// String read/write
+    def readStr(n Int)   -> Str
+    def readLnStr -> Str
+    
+    def writeStr   (data Str)
+    def writeLnStr (data Str)
+
+    
+
+//@rt__
+//class Dir(dirName Str):
+    //def open(mode Mode) -> Bool
+    //def close
+
+    
+
+/// Open file with specified mode
+def open(fileName Str, mode Mode = Mode.Read) -> File:
+    val handle = File(fileName)
+    handle.openText(mode)
     return handle
     modes = ['r', 'w', 'rw']
     return modes[x]
 
+Error = IOError
+
 class File(lang.Class):
     @staticmethod
     def new(filename): return File(filename)
     def __init__(self, filename):
         self.filename = filename
 
-    def open(self, mode): self.handle = open(self.filename, _modeToStr(mode))
+    def openText(self, mode): self.handle = open(self.filename, _modeToStr(mode))
+    def openBinary(self, mode): self.handle = open(self.filename, _modeToStr(mode))
     def close(self): self.handle.close()
-    def write(self, data): self.handle.write(data)
-    def writeLn(self, data): self.handle.writelines([data])
+    
+    def writeStr(self, data): 
+        self.handle.write(data)
+        return self
+    
+    def writeLnStr(self, data): 
+        self.handle.writelines([data + '\n'])
+        return self
+    
+    def readLnStr(self):
+        return self.handle.readline()

src/pygen/generator.py

     def runtime(self, modQName, node):
         """ create runtime spesific code to fullfil the body of definition """
          
-        rtTable = {'std.io': ['File']}
-        exists  = node.name in rtTable.get(modQName,[])
-            
-        if not exists:
+        rtTable = {
+            'std.io': [ ('File', self._rtExtend), ('Error', self._rtAlias)],
+            'std.exc': [ ('Error', self._rtAlias) ]
+        }
+        
+        action = False
+        xs    = rtTable.get(modQName,[])
+        for (name, method) in xs:
+            if name == node.name:
+                action = method
+                break
+        
+        if action == False:
             raise CodeGenError(
                'Compiler cannot generate the code for {name} because the spesific runtime code does not exist.'.format(
                 name=modQName+"."+node.name))
-                     
-        pyname   = self.toPyName(node)
+            
         pymodule = modQName.replace('std','rt') 
-        extend   = pymodule + '.' + node.name
+        alias   = pymodule + '.' + node.name
         
         ## insert the dependency modules
         self.__depImports.add(pymodule)
         
+        action(node, alias)
+
+             
+    def _rtExtend(self, node, alias):
         ## insert empty class declaration
-        with self.wrap(PyClass(pyname, [extend])):
+        with self.wrap(PyClass(node.name, [alias])):
             self.insert(PyPassStmt())
-             
+    
+    def _rtAlias(self, node, alias):
+        ## use assign
+        with self.wrap(PyAssignStmt('=')):
+            self.var(node.name)
+            self.var(alias)
+        
     
     ##########################################################
     ## visitor

tests/basics/t074_class5.tg

+module t074_class5
+
+import t072_class3 as phone
+
+// Testing inheritance from other module
+
+class MyHandPhone < phone.Android
+
+def main:
+    val hp = MyHandPhone()
+    

tests/basics/t180_exception.tg

 def main:
     try:
         io.print("inside try ")
-        raise exc.Exception()
-    except exc.Exception:
+        raise exc.Error("some error...")
+        io.print("not printed")
+    except exc.Error:
         io.print("inside exception ")
     finally:
         io.printLn("finally")
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.