Anonymous avatar Anonymous committed 5a7d2d2 Draft

This isn't much tidier. Hardly worth doing except to pass tests.

Comments (0)

Files changed (2)

src/castile/backends/stackmac.py

         self.loop_end = None
         self.fun_lit = None
         self.fun_argcount = 0
-        # 0 = print, 
-        self.global_pos = 1     # globals at the bottom of the stack
+        self.global_pos = 0     # globals at the bottom of the stack
         self.local_pos = 0      # locals after the passed arguments
 
     def get_label(self, pref):
             self.out.write("""\
 ; AUTOMATICALLY GENERATED -- EDIT AT OWN RISK
 
-print_index=0
-
-jmp past_print
-print:
-sys_print
-rts
-past_print:
-push print
-
 """)
             for child in ast.children:
                 self.compile(child)
             self.compile(ast.children[1])
             self.out.write('%s\n' % OPS.get(ast.value, ast.value))
         elif ast.type == 'VarRef':
-            if ast.aux in ('toplevel', 'global'):
-                self.out.write('get_global %s_index\n' % (ast.value))
+            if ast.aux == 'global':
+                self.out.write('builtin_%s\n' % ast.value)
+            elif ast.aux == 'toplevel':
+                self.out.write('get_global %s_index\n' % ast.value)
             else:
                 self.out.write('get_local %s_local_%s\n' % (self.fun_lit, ast.value))
         elif ast.type == 'FunCall':

src/castile/stackmac.py

 
 from castile.builtins import BUILTINS
 from castile.eval import TaggedValue
+from castile.types import Void
 
 
 labels = {}
         elif op == 'jmp':
             ip = arg - 1
         elif op == 'call':
-            callstack.append(ip)
-            ip = stack.pop() - 1
+            if isinstance(stack[-1], int):
+                callstack.append(ip)
+                ip = stack.pop() - 1
+            else:  # builtin
+                # TODO arity, convert strings... rrrrhhhh.
+                (builtin, type) = stack.pop()
+                a = stack.pop()
+                a = strings[a]
+                result = builtin(a)
+                if type.return_type != Void():
+                    stack.append(result)
         elif op == 'rts':
             ip = callstack.pop()
         elif op == 'mul':
                 struct.append(stack.pop())
                 x += 1
             stack.append(struct)
-        elif op == 'sys_print':
-            str_id = stack.pop()
-            print strings[str_id]
+        elif op.startswith('builtin_'):
+            (builtin, type) = BUILTINS[op[8:]]
+            stack.append((builtin, type))
         else:
             raise NotImplementedError((op, arg))
         ip += 1
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.