+#include "object.h"
#include "interp.h"
#include "tokens.h"
-#include "object.h"
extern int yylex();
extern char *yytext;
int compile_image(FILE *fp)
{
struct Object* ast = clone_object(list_object);
- expression(ast);
- display_ast(ast);
+ struct Object* first_leaf = clone_object(list_object);
+ list_append(ast, first_leaf);
+
+ expression(first_leaf);
+ execute(ast);
}
int expression(struct Object *ast) {
switch(r) {
case NUMBER:
obj = clone_object(number_object);
+ obj->value.i_value = atoi(yytext);
list_append(ast, obj);
return message(ast);
break;
break;
case SEMICOLON:
+ obj = clone_object(base_object);
+ obj->type = T_SEPARATOR;
+ obj->name = "Separator";
+ list_append(ast, obj);
return 0;
break;
}
}
+void display_object(struct Object *obj) {
+ printf("type: %d name: %s\n", obj->type, obj->name);
+}
+
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 length = mojo_list_length(statement_list->value.l_value);
+ struct Object *destObject = (struct Object*) list_nth(statement_list, 0);
+ struct Object *message = (struct Object*) list_nth(statement_list, 1);
+ struct Object *arg1 = (struct Object*) list_nth(statement_list, 2);
+
+ struct Object *method = (struct Object*) lookup_method(destObject, message->name);
+ if(method == nil_object) {
+ fail("error : method %s not found in object %s", message->name, destObject->name);
+ }
+ struct Object *ret = method->value.c_method(destObject, arg1, NULL, NULL, NULL);
+ printf("value %d", ret->value.i_value);
+ }
}
number_object->value.i_value = 0;
struct Object *plus_met = clone_object(builtin_object);
- plus_met->name = "+";
+ plus_met->name = "plus";
plus_met->value.c_method = (struct Object * (*)(struct Object *parent, struct Object *arg1,
struct Object *arg2))num_plus;