summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: ee7fd93)
raw | patch | inline | side by side (parent: ee7fd93)
author | DeaDDooMER <deaddoomer@deadsoftware.ru> | |
Wed, 2 Aug 2017 12:18:30 +0000 (15:18 +0300) | ||
committer | DeaDDooMER <deaddoomer@deadsoftware.ru> | |
Wed, 2 Aug 2017 12:18:30 +0000 (15:18 +0300) |
index 524b4ae19bef0e3b510872eb073fa2d1e6703567..0675bf305a87efbe7d223f9c0021289588676a9d 100644 (file)
--- a/notes
+++ b/notes
-- нет символов и строк
- нету типа set
-
- нету операторов if, while и т.д.
- Нужен тип представляющий типы
index 24c28b057429313018bf1529c64154c1e2a27825..88de2e2292119fbe5c8a4b090caee7c1051bc526 100644 (file)
return jvm_get_class_full_name(type);
break;
case OBERON_TYPE_ARRAY:
- return new_string("A%sX", jvm_get_descriptor_safe(type -> base));
+ return new_string("A%s", jvm_get_descriptor_safe(type -> base));
break;
default:
return jvm_get_descriptor(type);
index d609ed8de96c216a30ac4ec9bceda90ec230583e..f111fd16eff755440676a4b6a6e433713cc43b4b 100644 (file)
}
}
+static void
+jvm_generate_push_string(gen_proc_t * p, char * str, int char_size)
+{
+ assert(char_size == 1);
+ int len = strlen(str);
+
+ jvm_generate_push_int(p, len + 1);
+ jvm_generate(p, 1, 1, "newarray byte");
+
+ for(int i = 0; i < len; i++)
+ {
+ jvm_generate(p, 1, 2, "dup");
+ jvm_generate_push_int(p, i);
+ jvm_generate_push_int(p, str[i]);
+ jvm_generate(p, 3, 0, "bastore");
+ }
+}
+
static gen_var_t *
oberon_generator_new_var()
{
* Входящие параметры заграблены.
* Теперь генерируем эквивалентный код:
* int i = 0;
- * int len = dst.length
+ * int len = src.length
* while(i < len)
* {
* ...
jvm_generate(p, 0, 1, "iconst_0");
jvm_generate(p, 1, 0, "istore %i", loop[i].index -> reg);
- jvm_generate_load(p, arr, dst);
+ jvm_generate_load(p, arr, src);
jvm_generate(p, 1, 1, "arraylength");
jvm_generate(p, 1, 0, "istore %i", loop[i].length -> reg);
case OBERON_TYPE_ARRAY:
case OBERON_TYPE_REAL:
case OBERON_TYPE_CHAR:
+ case OBERON_TYPE_STRING:
break;
case OBERON_TYPE_RECORD:
;
case MODE_REAL:
jvm_generate_push_float(p, item -> real, item -> result -> size);
break;
+ case MODE_STRING:
+ jvm_generate_push_string(p, item -> string, item -> result -> size);
+ break;
default:
gen_error("push_item: unk mode %i", item -> mode);
break;
diff --git a/src/oberon-internals.h b/src/oberon-internals.h
index c154d0127093d265ed6c970614f21549f9a0f557..0b453494942f4ddc431035a8d99e4c511538d0bc 100644 (file)
--- a/src/oberon-internals.h
+++ b/src/oberon-internals.h
OBERON_TYPE_RECORD,
OBERON_TYPE_POINTER,
OBERON_TYPE_REAL,
- OBERON_TYPE_CHAR
+ OBERON_TYPE_CHAR,
+ OBERON_TYPE_STRING
};
typedef oberon_expr_t * (*GenerateFuncCallback)(oberon_context_t *, int, oberon_expr_t *);
oberon_type_t * real_type;
oberon_type_t * longreal_type;
oberon_type_t * char_type;
+ oberon_type_t * string_type;
oberon_scope_t * world_scope;
oberon_module_t * module_list;
MODE_NIL,
MODE_NEW,
MODE_REAL,
- MODE_CHAR
+ MODE_CHAR,
+ MODE_STRING
};
enum oberon_operator_kind
long integer;
double real;
int boolean;
+ char * string;
oberon_object_t * var;
oberon_item_t * parent;
diff --git a/src/oberon.c b/src/oberon.c
index 6cc5d1d193787867c68e74c0aec302d40a6022e0..f2310db26de0ab2b1bffb82efee2a4b346bf7052 100644 (file)
--- a/src/oberon.c
+++ b/src/oberon.c
NIL,
IMPORT,
REAL,
- CHAR
+ CHAR,
+ STRING
};
// =======================================================================
return x;
}
+static oberon_type_t *
+oberon_new_type_string(int size)
+{
+ oberon_type_t * x;
+ x = oberon_new_type_ptr(OBERON_TYPE_STRING);
+ x -> size = size;
+ return x;
+}
+
// =======================================================================
// TABLE
// =======================================================================
}
}
+static void oberon_read_string(oberon_context_t * ctx)
+{
+ int c = ctx -> c;
+ oberon_get_char(ctx);
+
+ int start = ctx -> code_index;
+
+ while(ctx -> c != 0 && ctx -> c != c)
+ {
+ oberon_get_char(ctx);
+ }
+
+ if(ctx -> c == 0)
+ {
+ oberon_error(ctx, "unterminated string");
+ }
+
+ int end = ctx -> code_index;
+
+ oberon_get_char(ctx);
+
+ char * string = calloc(1, end - start + 1);
+ strncpy(string, &ctx -> code[start], end - start);
+
+ ctx -> token = STRING;
+ ctx -> string = string;
+
+ printf("oberon_read_string: string ((%s))\n", string);
+}
+
static void oberon_read_token(oberon_context_t * ctx);
static void
ctx -> token = UPARROW;
oberon_get_char(ctx);
break;
+ case '"':
+ oberon_read_string(ctx);
+ break;
+ case '\'':
+ oberon_read_string(ctx);
+ break;
default:
oberon_error(ctx, "invalid char %c", ctx -> c);
break;
@@ -903,11 +949,29 @@ oberon_autocast_to(oberon_context_t * ctx, oberon_expr_t * expr, oberon_type_t *
// Допускается:
// Если классы типов равны
// Если INTEGER переводится в REAL
+ // Есди STRING переводится в ARRAY OF CHAR
bool error = false;
if(pref -> class != expr -> result -> class)
{
- if(expr -> result -> class == OBERON_TYPE_INTEGER)
+ printf("expr class %i\n", expr -> result -> class);
+ printf("pref class %i\n", pref -> class);
+
+ if(expr -> result -> class == OBERON_TYPE_STRING)
+ {
+ if(pref -> class == OBERON_TYPE_ARRAY)
+ {
+ if(pref -> base -> class != OBERON_TYPE_CHAR)
+ {
+ error = true;
+ }
+ }
+ else
+ {
+ error = true;
+ }
+ }
+ else if(expr -> result -> class == OBERON_TYPE_INTEGER)
{
if(pref -> class != OBERON_TYPE_REAL)
{
@@ -922,7 +986,7 @@ oberon_autocast_to(oberon_context_t * ctx, oberon_expr_t * expr, oberon_type_t *
if(error)
{
- oberon_error(ctx, "incompatible types");
+ oberon_error(ctx, "oberon_autocast_to: incompatible types");
}
if(pref -> class == OBERON_TYPE_INTEGER || pref -> class == OBERON_TYPE_REAL)
@@ -939,6 +1003,7 @@ oberon_autocast_to(oberon_context_t * ctx, oberon_expr_t * expr, oberon_type_t *
else if(pref -> class == OBERON_TYPE_RECORD)
{
oberon_check_record_compatibility(ctx, expr -> result, pref);
+ expr = oberno_make_record_cast(ctx, expr, pref);
}
else if(pref -> class == OBERON_TYPE_POINTER)
{
@@ -1154,6 +1219,7 @@ oberon_make_call_proc(oberon_context_t * ctx, oberon_object_t * proc, int num_ar
|| ((x) == INTEGER) \
|| ((x) == REAL) \
|| ((x) == CHAR) \
+ || ((x) == STRING) \
|| ((x) == NIL) \
|| ((x) == LPAREN) \
|| ((x) == NOT) \
break;
case INTEGER:
result = oberon_get_type_of_int_value(ctx, ctx -> integer);
- expr = oberon_new_item(MODE_INTEGER, result, 1);
+ expr = oberon_new_item(MODE_INTEGER, result, true);
expr -> item.integer = ctx -> integer;
oberon_assert_token(ctx, INTEGER);
break;
case CHAR:
result = ctx -> char_type;
- expr = oberon_new_item(MODE_CHAR, result, 1);
+ expr = oberon_new_item(MODE_CHAR, result, true);
expr -> item.integer = ctx -> integer;
oberon_assert_token(ctx, CHAR);
break;
+ case STRING:
+ result = ctx -> string_type;
+ expr = oberon_new_item(MODE_STRING, result, true);
+ expr -> item.string = ctx -> string;
+ oberon_assert_token(ctx, STRING);
+ break;
case REAL:
result = (ctx -> longmode) ? (ctx -> longreal_type) : (ctx -> real_type);
expr = oberon_new_item(MODE_REAL, result, 1);
oberon_assert_token(ctx, REAL);
break;
case TRUE:
- expr = oberon_new_item(MODE_BOOLEAN, ctx -> bool_type, 1);
+ expr = oberon_new_item(MODE_BOOLEAN, ctx -> bool_type, true);
expr -> item.boolean = true;
oberon_assert_token(ctx, TRUE);
break;
case FALSE:
- expr = oberon_new_item(MODE_BOOLEAN, ctx -> bool_type, 1);
+ expr = oberon_new_item(MODE_BOOLEAN, ctx -> bool_type, true);
expr -> item.boolean = false;
oberon_assert_token(ctx, FALSE);
break;
break;
case NIL:
oberon_assert_token(ctx, NIL);
- expr = oberon_new_item(MODE_NIL, ctx -> void_ptr_type, 1);
+ expr = oberon_new_item(MODE_NIL, ctx -> void_ptr_type, true);
break;
default:
oberon_error(ctx, "invalid expression");
ctx -> void_ptr_type -> base = ctx -> void_type;
oberon_generator_init_type(ctx, ctx -> void_ptr_type);
+ ctx -> string_type = oberon_new_type_string(1);
+ oberon_generator_init_type(ctx, ctx -> string_type);
+
ctx -> bool_type = oberon_new_type_boolean();
oberon_define_type(ctx -> world_scope, "BOOLEAN", ctx -> bool_type, 1);
diff --git a/src/test.c b/src/test.c
index d2950b968189c70a5f4b743c5131cd9682498959..cacf00d30d71189e19360a4b2467ba052f037858 100644 (file)
--- a/src/test.c
+++ b/src/test.c
#include "../include/oberon.h"
+/*
+static char source_test[] =
+ "(* Main module *)"
+ "MODULE Test;"
+ "IMPORT Out;"
+ "VAR"
+ " msg : ARRAY 20 OF CHAR;"
+ "BEGIN"
+ " msg := ''"
+ "END Test."
+;
+*/
+
static char source_test[] =
"(* Main module *)"
"MODULE Test;"
"IMPORT Out;"
"CONST"
+ " helloworld = 'Hello World!';"
" null = 0X;"
" space = 020X;"
" bang = 021X;"
" PrintString = PROCEDURE (str : ARRAY OF CHAR);"
""
"VAR"
- " hello : Ident;"
+ " msg : Ident;"
" print : PrintString;"
""
"BEGIN"
+ " msg := helloworld;"
" print := Out.String;"
- " hello[0] := h;"
- " hello[1] := e;"
- " hello[2] := l;"
- " hello[3] := l;"
- " hello[4] := o;"
- " hello[5] := space;"
- " hello[6] := w;"
- " hello[7] := o;"
- " hello[8] := r;"
- " hello[9] := l;"
- " hello[10] := d;"
- " hello[11] := bang;"
- " hello[12] := null;"
" Out.Open;"
- " print(hello);"
+ " print(msg);"
+ " Out.Ln;"
+ " print(\"It's works!\");"
" Out.Ln;"
"END Test."
;
-// PROCEDURE Char* (ch : CHAR);
-// PROCEDURE String* (str : ARRAY OF CHAR);
-
static char source_out[] =
"MODULE Out;"
" PROCEDURE Open*;"