--- /dev/null
+/* 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;
+
+ }
+ }
+}
break;
default:
+ printf ("------ %s -----", yytext);
/* method call, like Lexer yylex; */
puts("{"); /* put it in a block, to not disrupt other declarations */
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
def parse(s):
defs = [("lparen", "\("),
("rparen", "\)"),
+ ("semicolon", ";"),
("assignment", ":="),
("word", "[a-z]+"),
("number", "[0-9]+"),]
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
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)
--- /dev/null
+#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);
+}
--- /dev/null
+/* string object implementation */
+#ifndef __MOJO_STRINGS__
+#define __MOJO_STRINGS__
+
+
+
+#endif
/* Everything is supposed to be under control */
%}
z := truc
+s := 3
Lexer yylex;