* (: setPosx 3)))
*/
+
#include "object.h"
#include "interp.h"
#include "tokens.h"
+#include "list.h"
+#include "error.h"
extern int yylex();
+extern int yyleng;
+extern FILE *yyin;
extern char *yytext;
int compile_image(FILE *fp)
{
- struct Object* ast = clone_object(list_object);
-
- expression(ast);
- display_ast(ast, 0);
- puts(">>> AST <<<");
+
+ while(!feof(fp)) {
+ struct Object* ast = clone_object(list_object);
+ struct Object* scope = clone_object(list_object);
+
+ expression(ast);
+ display_ast(ast, 0);
+ execute_branch(ast, scope);
+ free_object(ast);
+ }
+
+ return 0;
+}
- struct Object *returnValue = execute_branch(ast);
- printf("Value : %d", returnValue->value.i_value);
+/*
+void unput_token(void) {
+ int i;
+ for (i = yyleng - 1; i >= 0; --i )
+ unput(yytext[i]);
}
+*/
int expression(struct Object *ast) {
params = clone_object(list_object);
list_append(params, obj);
list_append(ast, params);
-
return message(params);
break;
+
+ case SYMBOL:
+ obj = clone_object(base_object);
+ obj->type = T_SYMBOL;
+ obj->name = strdup(yytext);
+ params = clone_object(list_object);
+ list_append(params, obj);
+ list_append(ast, params);
+ return message(params);
+
+ break;
default:
fail("Unexpected : %s", yytext);
+ return -1;
break;
}
}
int r = yylex();
struct Object *obj;
- switch(r) {
- case SYMBOL:
+ if (r == SYMBOL) {
obj = clone_object(base_object);
obj->type = T_MESSAGE;
obj->name = strdup(yytext);
list_append(ast, obj);
expression(ast);
- break;
-
- case SEMICOLON:
+ } else if (r == SEMICOLON) {
return 0;
- break;
-
- default:
+ } else {
fail("Expected symbol, got : %s", yytext);
- break;
+ return -1;
}
+ return -1;
}
void display_ast(struct Object *ast, int nindents) {
}
}
+const char *type_to_string(enum o_types e)
+{
+ switch(e) {
+ case T_NIL:
+ return "Nil";
+ break;
+
+ case T_BOOL:
+ return "Boolean";
+ break;
+
+ case T_NUMBER:
+ return "Number";
+ break;
+
+ case T_SYMBOL:
+ return "Symbol";
+ break;
+
+ case T_LIST:
+ return "List";
+ break;
+
+ case T_STRING:
+ return "String";
+ break;
+
+ case T_OBJECT:
+ return "Object";
+ break;
+
+ case T_BUILTIN:
+ return "Builtin function";
+ break;
+
+ case T_BLOCK:
+ return "Block";
+ break;
+
+ case T_MESSAGE:
+ return "Message";
+ break;
+
+ case T_SEPARATOR:
+ return "Separator";
+ break;
+
+ default:
+ return "Unknkown object";
+ break;
+ }
+}
+
void display_object(struct Object *obj, int nindents) {
int i = 0;
for (i = 0; i < nindents; i++)
putc(' ', stdout);
- printf("class: %s\n", obj->name);
+ printf("class: %s, type: %s\n", obj->name, type_to_string(obj->type));
if(obj->type == T_LIST) {
display_ast(obj, nindents + 4);
}
}
-void execute(struct Object *ast) {
- int length = mojo_list_length(ast->value.l_value);
-
- int i = 0;
- for(i = 0; i < length; i++) {
- struct Object *statement_list = (struct Object*) list_nth(ast, i);
-
- int statement_length = mojo_list_length(statement_list->value.l_value);
- int j = 0;
-
- // returnValue holds the result of the evaluation of an expression
- struct Object *returnValue = (struct Object*) list_nth(statement_list, statement_length - 1);
- struct Object *destObject, *method;
- for(j = statement_length - 2; j >= 0; j--) {
- struct Object *obj = (struct Object*) list_nth(statement_list, j);
- switch(obj->type) {
- case T_MESSAGE:
- if (j - 1 > 0) { // guard
- destObject = (struct Object*) list_nth(statement_list, j - 1);
- method = (struct Object*) lookup_method(destObject, obj->name);
- if(method == nil_object) {
- fail("error : method %s not found in object %s", obj->name, destObject->name);
- }
-
- returnValue = method->value.c_method(destObject, returnValue, NULL, NULL, NULL);
- }
-
- break;
- }
-
-
- }
- printf("value %d", returnValue->value.i_value);
- }
-}
-
-struct Object* execute_branch(struct Object *ast) {
+struct Object* execute_branch(struct Object *ast, struct Object *scope) {
if (ast == NULL || ast->type != T_LIST)
- return;
+ return NULL;
struct Object* stack = clone_object(list_object);
int length = mojo_list_length(ast->value.l_value);
if (length == 0)
- return;
+ return NULL;
// first, execute all the sub-expressions
continue;
if(obj->type == T_LIST) {
- list_append(stack, execute_branch(obj));
+ list_append(stack, execute_branch(obj, scope));
} else {
list_append(stack, obj);
}
} else if (length > 1) {
struct Object *destObject = list_nth(stack, 0);
struct Object *methodName = list_nth(stack, 1);
-
- struct Object *method = (struct Object*) lookup_method(destObject, methodName->name);
- if(method == nil_object) {
- fail("error : method %s not found in object %s", methodName->name, destObject->name);
- }
+
+
+ if(destObject->type == T_SYMBOL && strcmp(methodName->name, ":=") == 0) {
+ // this is an assignment
+ puts("assignment");
+ struct Object *value = list_nth(stack, 2);
+ struct Object* var = clone_object(base_object);
+ var->type = T_SYMBOL;
+ var->name = strdup(destObject->name);
+ var->value.v_value = clone_object(value);
+ list_append(scope->symtab, var);
+
+ } else {
+ // this is a regular method call
+ struct Object *method = (struct Object*) lookup_method(destObject, methodName->name);
+ if(method == nil_object) {
+ fail("error : method %s not found in object %s", methodName->name, destObject->name);
+ }
- /* we use the fact that if list_nth doesn't find an
- * object because it doesn't exists at the index, it returns nil
- */
- struct Object *param1 = list_nth(stack, 2),
- *param2 = list_nth(stack, 3),
- *param3 = list_nth(stack, 4),
- *param4 = list_nth(stack, 5);
+ /* we use the fact that if list_nth doesn't find an
+ * object because it doesn't exists at the index, it returns nil
+ */
+ struct Object *param1 = list_nth(stack, 2),
+ *param2 = list_nth(stack, 3),
+ *param3 = list_nth(stack, 4),
+ *param4 = list_nth(stack, 5);
+ if (param1->type == T_SYMBOL) {
+ struct Object *o = lookup_variable(scope, param1->name);
+ if (o != nil_object)
+ param1 = o;
+ }
+
- destObject = method->value.c_method(destObject, param1, param2, param3, param4);
+ destObject = method->value.c_method(destObject, param1, param2, param3, param4);
+ }
- // free the stack and the ast
- free_object(ast);
+ puts("freeing stuff");
free_object(stack);
+
return destObject;
}
#include <stdio.h>
#include <string.h>
#include "object.h"
+#include "list.h"
+#include "error.h"
struct Object* lookup_method(struct Object *o, char *name)
{
struct Object *met;
int i;
- for (i = 0; i < mojo_list_length(o->methods->value.l_value); i++) {
- met = (struct Object *) list_nth(o->methods, i);
- if (strcmp(name, met->name) == 0)
- return met;
+ if (o->methods != NULL && o->methods->value.l_value != NULL)
+ for (i = 0; i < mojo_list_length(o->methods->value.l_value); i++) {
+ met = (struct Object *) list_nth(o->methods, i);
+ if (strcmp(name, met->name) == 0)
+ return met;
+ }
+
+ return nil_object;
+}
+
+struct Object* lookup_variable(struct Object *scope, char *name)
+{
+ if (name == NULL || scope == NULL)
+ return NULL;
+
+ struct Object *var;
+
+ int i;
+ for (i = 0; i < mojo_list_length(scope->symtab->value.l_value); i++) {
+ var = (struct Object *) list_nth(scope->symtab, i);
+ if (strcmp(name, var->name) == 0)
+ return var;
}
return nil_object;
new->type = o->type;
new->name = strndup(o->name, 255); /* FIXME : test the value of strdup */
new->methods = (struct Object *) create_list_object();
+ new->symtab = (struct Object *) create_list_object();
struct Object *met;
}
break;
-
+ default:
+ break;
}
return new;
}
void free_object(struct Object *o)
{
- if (o == NULL || o == nil_object || o == base_object || o == number_object)
+ if (o == NULL || o == nil_object || o == base_object || o == number_object || o == list_object)
return;
+ /*
+ if (o->type == T_BUILTIN)
+ // don't free builtin methods
+ return;
+ */
/*
* FIXME: use our own lib for strings and don't use
* BSS strings which can't be freed.
- free(o->name);
*/
+
+ free(o->name);
+
if (o->methods != NULL)
free_object(o->methods);
free(o);
}
+void create_base_object(void) {
+ /* init base object */
+ base_object = new_object();
+ base_object->super = nil_object;
+ base_object->type = T_OBJECT;
+ base_object->name = "Object";
+}
+
void init_object_system(void)
{
/* We need to create the list object before everything else because it is
nil_object->type = T_NIL;
nil_object->name = "nil";
- /* init base object */
- base_object = new_object();
- base_object->super = nil_object;
- base_object->type = T_OBJECT;
- base_object->name = "Object";
+ create_base_object();
/* init builtin object */
builtin_object = new_object();