From 5faf4435786c2b16de6a744c41da010dd4963dfa Mon Sep 17 00:00:00 2001 From: khamidou Date: Thu, 11 Nov 2010 16:54:07 +0100 Subject: [PATCH] beefing up the interpreter --- bytecode.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++ compiler.c | 1 + compiler.py | 23 ++++++++++++++++-- mstrings.c | 17 +++++++++++++ mstrings.h | 7 ++++++ tests/basic.mj | 1 + 6 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 bytecode.c create mode 100644 mstrings.c create mode 100644 mstrings.h diff --git a/bytecode.c b/bytecode.c new file mode 100644 index 0000000..f899e37 --- /dev/null +++ b/bytecode.c @@ -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; + + } + } +} diff --git a/compiler.c b/compiler.c index 38fd5c2..7025915 100644 --- a/compiler.c +++ b/compiler.c @@ -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 */ diff --git a/compiler.py b/compiler.py index c738aa4..24ab6a1 100644 --- a/compiler.py +++ b/compiler.py @@ -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 index 0000000..a1bdeea --- /dev/null +++ b/mstrings.c @@ -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 index 0000000..a9caf48 --- /dev/null +++ b/mstrings.h @@ -0,0 +1,7 @@ +/* string object implementation */ +#ifndef __MOJO_STRINGS__ +#define __MOJO_STRINGS__ + + + +#endif diff --git a/tests/basic.mj b/tests/basic.mj index 91479de..b14645c 100644 --- a/tests/basic.mj +++ b/tests/basic.mj @@ -2,4 +2,5 @@ /* Everything is supposed to be under control */ %} z := truc +s := 3 Lexer yylex; -- 2.43.0