diff --git a/oberon.c b/oberon.c
index 440364361e45956f86091d3022fd50b3099f1026..98928fbe7c4e1629b2519b0f505e54fb921ef63b 100644 (file)
--- a/oberon.c
+++ b/oberon.c
// TABLE
// =======================================================================
-static oberon_type_t *
-oberon_find_type(oberon_context_t * ctx, char * name)
+static oberon_scope_t *
+oberon_open_scope(oberon_context_t * ctx)
+{
+ oberon_scope_t * scope = malloc(sizeof *scope);
+ memset(scope, 0, sizeof *scope);
+
+ oberon_object_t * list = malloc(sizeof *list);
+ memset(list, 0, sizeof *list);
+
+ scope -> ctx = ctx;
+ scope -> list = list;
+ scope -> up = ctx -> decl;
+
+ ctx -> decl = scope;
+ return scope;
+}
+
+static void
+oberon_close_scope(oberon_scope_t * scope)
+{
+ oberon_context_t * ctx = scope -> ctx;
+ ctx -> decl = scope -> up;
+}
+
+static oberon_object_t *
+oberon_define_object(oberon_scope_t * scope, char * name, int class)
{
- oberon_type_t * x = ctx -> types;
+ oberon_object_t * x = scope -> list;
while(x -> next && strcmp(x -> next -> name, name) != 0)
{
x = x -> next;
}
- return x -> next;
+ if(x -> next)
+ {
+ oberon_error(scope -> ctx, "already defined");
+ }
+
+ oberon_object_t * newvar = malloc(sizeof *newvar);
+ memset(newvar, 0, sizeof *newvar);
+ newvar -> name = name;
+ newvar -> class = class;
+
+ x -> next = newvar;
+
+ return newvar;
}
-static oberon_var_t *
-oberon_find_var(oberon_context_t * ctx, char * name)
+static oberon_object_t *
+oberon_find_object_in_list(oberon_object_t * list, char * name)
{
- oberon_var_t * x = ctx -> mod -> vars;
+ oberon_object_t * x = list;
while(x -> next && strcmp(x -> next -> name, name) != 0)
{
x = x -> next;
}
-
return x -> next;
}
+static oberon_object_t *
+oberon_find_object(oberon_scope_t * scope, char * name)
+{
+ oberon_object_t * result = NULL;
+
+ oberon_scope_t * s = scope;
+ while(result == NULL && s != NULL)
+ {
+ result = oberon_find_object_in_list(s -> list, name);
+ s = s -> up;
+ }
+
+ if(result == NULL)
+ {
+ oberon_error(scope -> ctx, "undefined ident %s", name);
+ }
+
+ return result;
+}
+
static void
-oberon_define_var(oberon_context_t * ctx, char * name, oberon_type_t * type)
+oberon_define_type(oberon_scope_t * scope, char * name, oberon_type_t * type)
{
- oberon_var_t * x = ctx -> mod -> vars;
- while(x -> next && strcmp(x -> next -> name, name) != 0)
+ oberon_object_t * id;
+ id = oberon_define_object(scope, name, OBERON_CLASS_TYPE);
+ id -> type = type;
+ oberon_generator_init_type(scope -> ctx, type);
+}
+
+static oberon_type_t *
+oberon_find_type(oberon_scope_t * scope, char * name)
+{
+ oberon_object_t * x = oberon_find_object(scope, name);
+ if(x -> class != OBERON_CLASS_TYPE)
{
- x = x -> next;
+ oberon_error(scope -> ctx, "%s not a type", name);
}
- if(x -> next)
+ return x -> type;
+}
+
+static void
+oberon_define_var(oberon_scope_t * scope, char * name, oberon_type_t * type)
+{
+ oberon_object_t * var;
+ var = oberon_define_object(scope, name, OBERON_CLASS_VAR);
+ var -> type = type;
+ oberon_generator_init_var(scope -> ctx, var);
+}
+
+/*
+static oberon_object_t *
+oberon_find_var(oberon_scope_t * scope, char * name)
+{
+ oberon_object_t * x = oberon_find_object(scope, name);
+
+ if(x -> class != OBERON_CLASS_VAR)
{
- oberon_error(ctx, "already defined");
+ oberon_error(scope -> ctx, "%s not a var", name);
}
- oberon_var_t * newvar = malloc(sizeof *newvar);
- memset(newvar, 0, sizeof *newvar);
- newvar -> name = name;
- newvar -> type = type;
- oberon_generator_init_var(ctx, newvar);
+ return x;
+}
+*/
- x -> next = newvar;
+static oberon_object_t *
+oberon_define_proc(oberon_scope_t * scope, char * name)
+{
+ oberon_object_t * proc;
+ proc = oberon_define_object(scope, name, OBERON_CLASS_PROC);
+ oberon_generator_init_proc(scope -> ctx, proc);
+ return proc;
}
// =======================================================================
oberon_factor(oberon_context_t * ctx)
{
char * name;
- oberon_var_t * var;
+ oberon_object_t * var;
oberon_expr_t * expr;
switch(ctx -> token)
{
case IDENT:
name = oberon_assert_ident(ctx);
- var = oberon_find_var(ctx, name);
- if(var == NULL)
+ var = oberon_find_object(ctx -> decl, name);
+ if(var -> class == OBERON_CLASS_VAR)
{
- oberon_error(ctx, "undefined variable %s", name);
+ expr = oberon_new_item(MODE_VAR, var -> type);
}
- expr = oberon_new_item(MODE_VAR, var -> type);
+ else if(var -> class == OBERON_CLASS_PROC)
+ {
+ expr = oberon_new_item(MODE_CALL, var -> type);
+ }
+ else
+ {
+ oberon_error(ctx, "invalid designator");
+ }
+
expr -> item.var = var;
+ if(ctx -> token == LPAREN)
+ {
+ oberon_assert_token(ctx, LPAREN);
+ expr -> item.mode = MODE_CALL;
+ oberon_assert_token(ctx, RPAREN);
+ }
break;
case INTEGER:
expr = oberon_new_item(MODE_INTEGER, ctx -> int_type);
oberon_type(oberon_context_t * ctx)
{
char * name = oberon_assert_ident(ctx);
- oberon_type_t * type = oberon_find_type(ctx, name);
-
- if(type == NULL)
- {
- oberon_error(ctx, "undefined type");
- }
-
+ oberon_type_t * type = oberon_find_type(ctx -> decl, name);
return type;
}
char * name = oberon_assert_ident(ctx);
oberon_assert_token(ctx, COLON);
oberon_type_t * type = oberon_type(ctx);
- oberon_define_var(ctx, name, type);
+ oberon_define_var(ctx -> decl, name, type);
}
static void
oberon_make_procedure_begin(oberon_context_t * ctx, char * name)
{
-
+ oberon_object_t * proc;
+ proc = oberon_define_proc(ctx -> decl, name);
+
+ oberon_open_scope(ctx);
+
+ oberon_generate_begin_proc(ctx, proc);
}
static void
oberon_make_procedure_end(oberon_context_t * ctx)
{
+ oberon_generate_end_proc(ctx);
+ oberon_close_scope(ctx -> decl);
}
static void
@@ -818,6 +919,29 @@ oberon_assign(oberon_context_t * ctx, oberon_expr_t * src, oberon_expr_t * dst)
oberon_generate_assign(ctx, src, dst);
}
+static void
+oberon_make_call(oberon_context_t * ctx, oberon_expr_t * desig)
+{
+ if(desig -> is_item == 0)
+ {
+ oberon_error(ctx, "expected item");
+ }
+
+ if(desig -> item.mode != MODE_CALL)
+ {
+ oberon_error(ctx, "expected mode CALL");
+ }
+
+ if(desig -> item.var -> class != OBERON_CLASS_PROC)
+ {
+ oberon_error(ctx, "only procedures can be called");
+ }
+
+ // TODO check arguments
+
+ oberon_generate_call_proc(ctx, desig);
+}
+
static void
oberon_statement(oberon_context_t * ctx)
{
if(ctx -> token == IDENT)
{
item1 = oberon_expr(ctx);
- oberon_assert_token(ctx, ASSIGN);
- item2 = oberon_expr(ctx);
- oberon_assign(ctx, item2, item1);
+ if(ctx -> token == ASSIGN)
+ {
+ oberon_assert_token(ctx, ASSIGN);
+ item2 = oberon_expr(ctx);
+ oberon_assign(ctx, item2, item1);
+ }
+ else
+ {
+ oberon_make_call(ctx, item1);
+ }
}
}
// =======================================================================
static oberon_type_t *
-oberon_register_global_type_ret(oberon_context_t * ctx, oberon_type_t * type)
+oberon_new_type_ptr(int class)
{
- oberon_type_t * x = ctx -> types;
- while(x -> next && strcmp(x -> next -> name, type -> name) != 0)
- {
- x = x -> next;
- }
-
- if(x -> next)
- {
- oberon_error(ctx, "already defined");
- }
+ oberon_type_t * x = malloc(sizeof *x);
+ memset(x, 0, sizeof *x);
+ x -> class = class;
+ return x;
+}
- // TODO: copy type name (not a pointer)
- oberon_type_t * newtype = malloc(sizeof *newtype);
- memcpy(newtype, type, sizeof *newtype);
- newtype -> next = NULL;
- oberon_generator_init_type(ctx, newtype);
+static oberon_type_t *
+oberon_new_type_integer(int size)
+{
+ oberon_type_t * x;
+ x = oberon_new_type_ptr(OBERON_TYPE_INTEGER);
+ x -> size = size;
+ return x;
+}
- x -> next = newtype;
- return newtype;
+static oberon_type_t *
+oberon_new_type_boolean(int size)
+{
+ oberon_type_t * x;
+ x = oberon_new_type_ptr(OBERON_TYPE_BOOLEAN);
+ x -> size = size;
+ return x;
}
static void
register_default_types(oberon_context_t * ctx)
{
- static oberon_type_t integer = { "INTEGER", OBERON_TYPE_INTEGER, sizeof(int) };
- static oberon_type_t boolean = { "BOOLEAN", OBERON_TYPE_BOOLEAN, sizeof(int) };
+ ctx -> int_type = oberon_new_type_integer(sizeof(int));
+ ctx -> bool_type = oberon_new_type_boolean(sizeof(int));
- ctx -> int_type = oberon_register_global_type_ret(ctx, &integer);
- ctx -> bool_type = oberon_register_global_type_ret(ctx, &boolean);
-}
-
-void
-oberon_register_global_type(oberon_context_t * ctx, oberon_type_t * type)
-{
- oberon_register_global_type_ret(ctx, type);
+ oberon_define_type(ctx -> world_scope, "INTEGER", ctx -> int_type);
+ oberon_define_type(ctx -> world_scope, "BOOLEAN", ctx -> bool_type);
}
oberon_context_t *
oberon_context_t * ctx = malloc(sizeof *ctx);
memset(ctx, 0, sizeof *ctx);
- oberon_type_t * types = malloc(sizeof *types);
- memset(types, 0, sizeof *types);
- ctx -> types = types;
+ oberon_scope_t * world_scope;
+ world_scope = oberon_open_scope(ctx);
+ ctx -> world_scope = world_scope;
oberon_generator_init_context(ctx);
{
oberon_module_t * mod = malloc(sizeof *mod);
memset(mod, 0, sizeof *mod);
- oberon_var_t * vars = malloc(sizeof *vars);
- memset(vars, 0, sizeof *vars);
ctx -> mod = mod;
- ctx -> mod -> vars = vars;
+
+ oberon_scope_t * module_scope;
+ module_scope = oberon_open_scope(ctx);
+ mod -> decl = module_scope;
oberon_init_scaner(ctx, code);
oberon_parse_module(ctx);
oberon_generate_code(ctx);
+
+ ctx -> mod = NULL;
return mod;
}
-