]> khamidou — GitHub mirrors - mojo.git/commitdiff
beefing up the interpreter
authorkhamidou <khamidou@gmail.com>
Thu, 11 Nov 2010 15:54:07 +0000 (16:54 +0100)
committerkhamidou <khamidou@gmail.com>
Thu, 11 Nov 2010 15:54:07 +0000 (16:54 +0100)
bytecode.c [new file with mode: 0644]
compiler.c
compiler.py
mstrings.c [new file with mode: 0644]
mstrings.h [new file with mode: 0644]
tests/basic.mj

diff --git a/bytecode.c b/bytecode.c
new file mode 100644 (file)
index 0000000..f899e37
--- /dev/null
@@ -0,0 +1,65 @@
+/* bytecode.c - bytecode interpretation 
+
+   the structure of the bytecode is as follows:
+   first, an opcode, followed by its parameters. The paramaters are separated
+   by commas. A dot signals the end of sequence.
+
+   The following opcodes are supported for the moment:
+   PUSH val : push a value on top of the stack
+   POP : remove the value on the top of the stack
+   SEND_UNARY: send a message to an object with one parameter. The order of the parameters on the stack is :
+               object, message, param, from top to bottom
+   SEND_BINARY: FIXME: yet to implement.
+   ASSIGN val 
+*/
+
+enum {PUSH, POP, SEND_UNARY, SEND_BINARY, NEW, ASSIGN}
+
+struct Object *stack;
+
+init vm(void)
+{
+       stack = create_list_object();
+}
+
+/* get a copy of the next word contained in the bytecode */
+char *get_next_bytecode_word(char **ptr)
+{
+       int len = 0;
+       char *c = *ptr;
+       while(*c != ',' && c!= '.' && c != 0) {
+               len++;
+               c++;
+       }
+
+       char retptr = calloc(len * sizeof(char), 1);
+
+       if (!retptr)  
+               fail("Unable to alloc in __FILE__:__LINE__");
+
+       c = *ptr;
+       while (*c != ',' && c!= '.' && c != 0) {
+               *retptr++ = *c++;
+       }
+
+       /* don't forget to advance ptr by length + 1. */
+       *ptr += len + 1;
+       return retptr;
+
+}              
+
+int execute_bytecode(char *ptr)
+{
+
+       char c;
+
+       while(*ptr) {
+               switch(*ptr) {
+                       case PUSH:
+                               c = get_next_word(&ptr);
+                               //stack->append(
+                               break;
+
+               }
+       }
+}
index 38fd5c27456909f51325b9edb7e334ab0a6bc913..702591584a7d21f63ef65c4668bccbff188c5a7d 100644 (file)
@@ -65,6 +65,7 @@ void symbol()
                break;
 
        default:
+               printf ("------ %s -----", yytext);
                /* method call, like Lexer yylex; */
                puts("{"); /* put it in a block, to not disrupt other declarations */
 
index c738aa483f9087d79b09c78b647a3b83b8780451..24ab6a138e58ef4dd54fc8ad852922d015ab77e0 100644 (file)
@@ -8,9 +8,11 @@ import re
 symtab = {}
 
 class Symbol:
-       def __init__(self, name, value):
+       def __init__(self, name, value=None):
                self.name = name
                self.value = value
+       def __repr__(self):
+               return "type: symbol, name: %s, value: %s" % (self.name, self.value)
 class Ast:
        def __init__(self, type, value):
                self.type = type
@@ -51,6 +53,7 @@ class Lexer(object):
 def parse(s):  
        defs = [("lparen", "\("),
                ("rparen", "\)"),
+               ("semicolon", ";"),
                ("assignment", ":="),
                ("word", "[a-z]+"),
                ("number", "[0-9]+"),]
@@ -72,8 +75,14 @@ def parse(s):
 
                        if ntok[0] == "assignment":
                                retast.method = ":="
+                               sym = Symbol(token[1])
+                               symtab[token[1]] = sym
                        elif ntok[0] == "word":
                                retast.method = ntok[1]
+                               # Scan the tokens until getting a semicolon
+                               retast.params = []
+                               while ((ntok = input.next()) != "semicolon")
+                                       retast.params.append(Ast(ntok[1]))
                        else:
                                error("Invalid token %s near %s" % (ntok[1], token[1]))
                        return retast
@@ -86,11 +95,21 @@ def parse(s):
        
        return ast
 
+def compile_ast(ast):
+       # First, output the variable declarations
+       for sym in symtab:
+               print "struct mojo_object *cl_%s;" % sym
+
+       for node in ast:
+               print node.methods
+
 if __name__ == "__main__":
        if len(sys.argv) != 2:
                print "%s file.mj" % sys.argv[0]
                sys.exit(0)
 
-       print parse(read_file(sys.argv[1]))
+       ast = parse(read_file(sys.argv[1]))
+       print ast
+       compile_ast(ast)
 
 
diff --git a/mstrings.c b/mstrings.c
new file mode 100644 (file)
index 0000000..a1bdeea
--- /dev/null
@@ -0,0 +1,17 @@
+#include "mstrings.h"
+
+void create_string_object(void)
+{
+       /* init number object */
+       number_object = clone_object(base_object);
+       number_object->type = T_STRING;
+       number_object->name = "String";
+       number_object->value.i_value = 0;
+       
+       struct Object *plus_met = clone_object(builtin_object);
+       plus_met->name = "+";
+       plus_met->value.c_method = (struct Object * (*)(struct Object *parent, struct Object *arg1, 
+                                                  struct Object *arg2))num_plus;
+
+       list_append(number_object->methods, plus_met);
+}
diff --git a/mstrings.h b/mstrings.h
new file mode 100644 (file)
index 0000000..a9caf48
--- /dev/null
@@ -0,0 +1,7 @@
+/* string object implementation */
+#ifndef __MOJO_STRINGS__
+#define __MOJO_STRINGS__
+
+
+
+#endif
index 91479de9df999116c0c11007e8124b7c851753b9..b14645c5ba57b0ab73173763c3be63f92562a4b7 100644 (file)
@@ -2,4 +2,5 @@
        /* Everything is supposed to be under control */
 %}
 z := truc
+s := 3
 Lexer yylex;