Commit MetaInfo

Revisión72dd81063264a6373e689d0d83c762a7ae4ef71d (tree)
Tiempo2014-07-27 18:56:26
Autorttwilb <ttwilb@user...>
Commiterttwilb

Log Message

式評価が動作

Cambiar Resumen

Diferencia incremental

--- a/asmi/expression.c
+++ b/asmi/expression.c
@@ -25,11 +25,12 @@ expression *alloc_plain_expression() {
2525 asmi_fatal("expressionを確保できません。");
2626 return NULL;
2727
28-}// http://www.google.com
28+}
2929
3030 struct {
3131 operation_id id;
3232 token_id tkn_id;
33+ asmi_type rtyp; // この式の型
3334 asmi_type etyp1; // 引数の必須条件
3435 asmi_type etyp2;
3536 asmi_type etyp3;
@@ -90,6 +91,99 @@ struct {
9091 {NIL, NIL, NIL, NIL, NIL, FALSE, FALSE}
9192 };
9293
94+// exp->value_type を設定するのが主な目的。
95+int generate_expression_sub(expression *exp, token_id op, expression *exp0, expression *exp1, expression *exp2) {
96+
97+ // エラーチェック
98+ switch (exp->id) {
99+
100+ case OP_ADDASG: case OP_SUBASG:case OP_MULASG: case OP_DIVASG: case OP_ORASG: case OP_ANDASG: case OP_XORASG:
101+ case OP_SHLASG: case OP_SARASG: case OP_MODASG: case OP_ADDFASG: case OP_SUBFASG: case OP_MULFASG: case OP_DIVFASG:
102+ case OP_INC: case OP_DEC:
103+ if (!exp0->is_leftval) {
104+ return -1;
105+ }
106+ break;
107+ case OP_INCA: case OP_DECA:
108+ if (!exp1->is_leftval) {
109+ return -1;
110+ }
111+ break;
112+ }
113+
114+ // value type の決定
115+ int bit0 = exp0 ? exp0->value_type & 0xFF : -1;
116+ int bit1 = exp1 ? exp1->value_type & 0xFF : -1;
117+ int bit2 = exp2 ? exp2->value_type & 0xFF : -1;
118+ int bit = 0;
119+
120+ switch (exp->id) {
121+
122+ // osecpu121d-vm integer.c:351
123+ case OP_LAND: case OP_LOR: case OP_XOR: case OP_ADDI:case OP_SUBI:case OP_MULI:
124+ case OP_XORASG:
125+ case OP_ADDASG: case OP_SUBASG: case OP_MULASG:
126+
127+ // osecpu121d-vm integer.c:391
128+ case OP_SHL: case OP_SAR:
129+ case OP_SHLASG: case OP_SARASG:
130+
131+ // osecpu121d-vm integer.c:411
132+ case OP_DIVI: case OP_MOD:
133+ case OP_DIVASG: case OP_MODASG:
134+
135+ case OP_ASG: case OP_NOT:
136+
137+ // 式のbitは引数のbitの多い方にそろえる
138+ bit = bit0 < bit1 ? bit1 : bit0;
139+ exp->value_type = T_PRIM | T_INT | bit;
140+ break;
141+
142+ // osecpu121d-vm integer.c:434
143+ case OP_CMPEQ: case OP_CMPNE: case OP_CMPLT: case OP_CMPGE: case OP_CMPLE: case OP_CMPGT:
144+
145+ // osecpu121d-vm float.c:207
146+ case OP_CMPEQF: case OP_CMPNEF: case OP_CMPLTF: case OP_CMPGEF: case OP_CMPLEF: case OP_CMPGTF:
147+
148+ bit = 32;
149+ exp->value_type = T_PRIM | T_INT | bit;
150+ break;
151+
152+ // osecpu121d-vm float.c:227
153+ case OP_ADDF: case OP_SUBF: case OP_MULF: case OP_DIVF:
154+ case OP_ADDFASG: case OP_SUBFASG: case OP_MULFASG: case OP_DIVFASG:
155+
156+ case OP_ASGF:
157+
158+ case OP_INC: case OP_DEC:
159+
160+ bit = bit0;
161+ exp->value_type = T_PRIM | T_INT | bit;
162+ break;
163+
164+ case OP_INCA: case OP_DECA:
165+
166+ bit = bit1;
167+ exp->value_type = T_PRIM | T_INT | bit;
168+ break;
169+
170+ case OP_OR: case OP_AND:
171+ case OP_ORASG: case OP_ANDASG:
172+
173+ bit = 32;
174+ exp->value_type = T_PRIM | T_INT | bit;
175+ break;
176+
177+ case OP_CNVIF:
178+ case OP_CNVFI:
179+ case OP_SBX:
180+
181+ break;
182+ }
183+
184+ return 0;
185+}
186+
93187 // 演算子と与えられた式から新しい式を生成する。
94188 // 引数に与えられた式の型が不正だと、NULLを返す。
95189 expression *generate_expression(token_id op, expression *exp0, expression *exp1, expression *exp2) {
@@ -100,7 +194,7 @@ expression *generate_expression(token_id op, expression *exp0, expression *exp1,
100194
101195 for (i = 0; expression_templete_table[i].id != NIL; i++) {
102196
103- if (op = expression_templete_table[i].id) {
197+ if (op == expression_templete_table[i].tkn_id) {
104198
105199 etyp1 = expression_templete_table[i].etyp1;
106200 etyp2 = expression_templete_table[i].etyp2;
@@ -139,6 +233,13 @@ expression *generate_expression(token_id op, expression *exp0, expression *exp1,
139233 exp->is_const = expression_templete_table[i].maybe_const && (exp0 == NULL || exp0->is_const) &&
140234 (exp1 == NULL || exp1->is_const) && (exp2 == NULL || exp2->is_const) ? TRUE : FALSE;
141235 exp->is_leftval = expression_templete_table[i].is_leftval;
236+
237+ // オペレーション別の処理を行う。
238+ if (generate_expression_sub(exp, op, exp0, exp1, exp2)) {
239+
240+ // エラー
241+ asmi_fatal("式にエラーがあります。");
242+ }
142243
143244 break;
144245 }
@@ -365,7 +466,7 @@ expression *evaluate_factor(input_stream *stream, asmi_context *context) {
365466 return rexp;
366467
367468 }
368-
469+char *get_op_string(operation_id id);
369470 expression *evaluate_term(input_stream *stream, asmi_context *context, int level) {
370471
371472 token tkn;
@@ -403,7 +504,6 @@ expression *evaluate_term(input_stream *stream, asmi_context *context, int level
403504
404505 // 演算子の後の項を評価する
405506 exp2 = evaluate_term(stream, context, level);
406-
407507 rexp = generate_expression(tkn.id, exp, exp2, NULL);
408508
409509 }
@@ -434,6 +534,9 @@ expression *evaluate_term(input_stream *stream, asmi_context *context, int level
434534 if (rexp == NULL) { // generate_expression() の失敗
435535 asmi_fatal("式が不正確です");
436536 }
537+ else {
538+ //printf("%s ", get_op_string(rexp->id));
539+ }
437540
438541 /*
439542
--- a/asmi/source.txt
+++ b/asmi/source.txt
@@ -1,3 +1,3 @@
11 
22
3-123 * 234 / 3232 * 1234 / 234.5
3+123 * 234 / 3232 * (1234 / 234);
--- a/asmi/token.c
+++ b/asmi/token.c
@@ -277,8 +277,7 @@ struct {
277277 token_id id;
278278 token_kind kind;
279279 }word_table[] = {
280- { "(", Lparen, TK_Other }, { ")", Rparen, TK_Other }, { "{", Lbrace, TK_Other }, { "}", Rbrace, TK_Other }, { "[", Lbracket, TK_Other }, { "]", Rbracket, TK_Other },
281- { ":", Colon, TK_Other }, { ";", Semicolon, TK_Other }, { "if", If, TK_Other }, { "for", For, TK_Other }, { "until", Until, TK_Other }, { "void", Void, TK_Type },
280+ { "if", If, TK_Other }, { "for", For, TK_Other }, { "until", Until, TK_Other }, { "void", Void, TK_Type },
282281 { "int", Int, TK_Type }, { "char", Char, TK_Type }, { "signed", Signed, TK_Type }, { "unsigned", Unsigned, TK_Type }, { "short", Short, TK_Type },
283282 { "return", Return, TK_Other }, { "do", Do, TK_Other }, { "else", Else, TK_Other }, { "long", Long, TK_Type }, { "float", Float, TK_Type }, { "double", Double, TK_Type },
284283 { "static", Static, TK_Other }, { "const", Const, TK_Other }, { "goto", Goto, TK_Other }, { "switch", Switch, TK_Other }, { "case", Case, TK_Other },
@@ -294,6 +293,8 @@ struct {
294293 token_id id;
295294 token_kind kind;
296295 }symbol_word_table[] = {
296+ { "(", 1, Lparen, TK_Other }, { ")", 1, Rparen, TK_Other }, { "{", 1, Lbrace, TK_Other }, { "}", 1, Rbrace, TK_Other },
297+ { "[", 1, Lbracket, TK_Other }, { "]", 1, Rbracket, TK_Other }, { ":", 1, Colon, TK_Other }, { ";", 1, Semicolon, TK_Other },
297298 { "+", 1, Plus, TK_Operator }, { "-", 1, Minus, TK_Operator },
298299 { "*", 1, Multi, TK_Operator }, { "/", 1, Divi, TK_Operator }, { "%", 1, Mod, TK_Operator }, { "!", 1, Not, TK_Operator },
299300 { "=", 1, Assign, TK_Operator }, { ",", 1, Comma, TK_Operator },
Show on old repository browser