+++ /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;
-
- }
- }
-}
+++ /dev/null
-/*
- The mojo boostrap compiler.
- copyright (c) 2009 karim hamidou.
-
- This is a rough translator for a subset of mojo to C.
- It does no verifications whatsoever, everything is kept at the sole discretion
- of the programmer.
-
-*/
-
-#include <stdio.h>
-#include <string.h>
-#include "compiler.h"
-
-#define SYMBOL_LENGTHS 255
-
-extern char *yytext;
-extern FILE *yyin;
-
-void symbol();
-
-int main(int argc, char **argv)
-{
- if (argc != 2) {
- fail("%s file", argv[0]);
- }
-
- yyin = fopen(argv[1], "r");
- if (yyin == NULL)
- fail("Unable to open %s", argv[1]);
-
- printf("%s", source_prologue);
-
- int r;
- while((r = yylex()) != T_EOF) {
- switch(r) {
- case EOF:
- break;
-
- case SYMBOL:
- symbol();
- break;
-
- default:
- break;
- }
- }
- printf("%s", source_epilogue);
-}
-
-
-void symbol()
-{
- char symname[SYMBOL_LENGTHS];
- strncpy(symname, yytext, SYMBOL_LENGTHS);
-
- int r = yylex();
- switch(r) {
- case EOF:
- break;
-
- case AFFECTATION:
- yylex();
- printf("struct Object *%s = %s;", symname, yytext);
- break;
-
- default:
- printf ("------ %s -----", yytext);
- /* method call, like Lexer yylex; */
- puts("{"); /* put it in a block, to not disrupt other declarations */
-
- printf("struct Object *met = lookup_method(%s, %s);\n", symname, yytext);
- printf("if (met != nil_object && met->type == T_BUILTIN)\n");
- printf(" met->c_method(%s,", symname);
-
- /* this is the most complex part of the algorithm :
- A call to c_method takes four arguments - as we don't know how much arguments we're gonna get,
- we add them on the fly until we get a comma, at which point we fill the remainder arguments
- with NULLs.
- */
-
- int i = 0;
-
- while ((r = yylex()) && i < 4) {
- switch(r) {
- case EOF:
- goto loop_exit;
- break;
-
- case COMMA:
- break;
-
- default:
- printf("%s,", yytext);
- break;
- }
- i++;
- }
-
- loop_exit:
- if (i < 4) {
- while(i < 3) {
- printf("NULL,");
- i++;
- }
- printf("NULL");
- }
- printf(");\n");
- puts("}");
- }
-}
+++ /dev/null
-#ifndef __COMPILER_H__
-#define __COMPILER_H__
-
-#include "tokens.h"
-#include "object.h"
-
-#define source_prologue "#include <stdio.h>\n\
-#include <stdlib.h>\n\
-#include \"object.h\"\n\
-\n\
-int main(int argc, char **argv) {\n\
- struct Object *Lexer;\n"
-
-#define source_epilogue "}"
-
-#endif
+++ /dev/null
-#!/usr/bin/env python
-#
-# A translator from mojo to C
-
-import sys
-import re
-
-symtab = {}
-
-class Symbol:
- 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
- self.value = value
- self.method = ""
-
- def __repr__(self):
- return "[type: %s, value: %s, method called: %s]" % (self.type, self.value, self.method)
-
-def read_file(file):
- return open(file).read()
-
-def error(msg):
- print msg
- sys.exit(-1)
-
-# The lexer class comes from http://www.gooli.org/blog/a-simple-lexer-in-python/
-class Lexer(object):
- def __init__(self, definitions):
- self.definitions = definitions
- parts = []
- for name, part in definitions:
- parts.append("(?P<%s>%s)" % (name, part))
- self.regexpString = "|".join(parts)
- self.regexp = re.compile(self.regexpString, re.MULTILINE)
-
- def parse(self, text):
- # yield lexemes
- for match in self.regexp.finditer(text):
- found = False
- for name, rexp in self.definitions:
- m = match.group(name)
- if m is not None:
- yield (name, m)
- break
-
-
-def parse(s):
- defs = [("lparen", "\("),
- ("rparen", "\)"),
- ("semicolon", ";"),
- ("assignment", ":="),
- ("word", "[a-z]+"),
- ("number", "[0-9]+"),]
-
-
- lex = Lexer(defs)
- input = lex.parse(s)
- prev = None
- ast = []
-
- def recparse(token):
- if token[0] == "word":
- retast = Ast("word", token[1])
-
- try:
- ntok = input.next()
- except StopIteration:
- return retast
-
- 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
-
- elif token[0] == "number":
- return Ast("number", token[1])
-
- for token in input:
- ast.append(recparse(token))
-
- 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)
-
- ast = parse(read_file(sys.argv[1]))
- print ast
- compile_ast(ast)
-
-