コミットメタ情報

リビジョンf82ed5f039e3eb7b5cf45ddff8b382a21bc47591 (tree)
日時2014-07-19 22:02:45
作者ttwilb <ttwilb@user...>
コミッターttwilb

ログメッセージ

式の評価。

変更サマリ

差分

--- a/asmi/asmi.c
+++ b/asmi/asmi.c
@@ -21,31 +21,30 @@ int asmi_main(int argc, char **argv){
2121 init_expression();
2222 init_tokenizer();
2323
24+ printf("***** TOKENS *****\n\n");
25+
2426 for (; (tkn = next_token(&stream, &context)).id != NIL;) {
2527
2628 debug_token(tkn);
2729
2830 }
31+ fclose(stream.fp);
2932
3033 // 第二ループ
3134
3235 context.defines_top = 0;
3336 context.token_stack_top = 0;
3437
35- // 引数が不足している場合はエラー。
36- if (argc == 1) {
37-
38- printf("Usage >%s file.asmi\n\n", "asmi.exe");
39- return -1;
40-
41- }
42-
4338 init_stream(&stream, argv[1]);
4439 init_expression();
4540 init_tokenizer();
4641
42+ printf("\n\n***** EXPRESSIONS *****\n\n");
43+
4744 parser_entry(&stream, &context);
4845
46+ fclose(stream.fp);
47+
4948 return 0;
5049
5150 }
--- a/asmi/asmi.h
+++ b/asmi/asmi.h
@@ -106,6 +106,8 @@ void pop_stream(input_stream *stream); // #include で読み込まれたフ
106106 // トークンの種類。これらの値はnext_token()でセットされる。
107107 typedef enum {
108108
109+ Null = NIL,
110+
109111 // 括弧
110112 Lparen = '(', Rparen = ')', Lbrace = '{', Rbrace = '}', Lbracket = '[', Rbracket = ']',
111113
@@ -133,15 +135,14 @@ typedef enum {
133135 Int, Char, Short, Long, Float, Double, Void, Signed, Unsigned,
134136
135137 // リテラル値
136- Ident, String, IntValue, FloatValue,
137-
138- Null = NIL
138+ Ident, String, IntValue, FloatValue
139139
140140 } token_id;
141141
142142 typedef enum {
143143
144- TK_Operator, TK_Type, TK_Other = NIL
144+ TK_Other = NIL,
145+ TK_Operator, TK_Type
145146
146147 } token_kind;
147148
@@ -245,6 +246,9 @@ typedef enum {
245246
246247 typedef enum {
247248
249+ // ヌルタイプ
250+ OP_Null = NIL,
251+
248252 // 単純演算
249253 OP_ADDI, OP_ADDF, OP_SUBI, OP_SUBF, OP_MULI, OP_MULF, OP_DIVI, OP_DIVF,
250254 OP_OR, OP_XOR, OP_AND, OP_SHL, OP_SAR, OP_CMPEQ, OP_CMPNE, OP_CMPLT, OP_CMPGT, OP_CMPLE, OP_CMPGE,
@@ -258,10 +262,7 @@ typedef enum {
258262
259263 OP_FACTOR,
260264
261- OP_CNVIF, OP_CNVFI, OP_SBX,
262-
263- // ヌルタイプ
264- OP_Null = NIL
265+ OP_CNVIF, OP_CNVFI, OP_SBX
265266
266267 } operation_id; // operator_typeに改名予定
267268
--- a/asmi/expression.c
+++ b/asmi/expression.c
@@ -25,7 +25,7 @@ expression *alloc_plain_expression() {
2525 asmi_fatal("expressionを確保できません。");
2626 return NULL;
2727
28-}
28+}// http://www.google.com
2929
3030 struct {
3131 operation_id id;
@@ -109,10 +109,27 @@ expression *generate_expression(token_id op, expression *exp0, expression *exp1,
109109
110110 if ((exp0 == NULL && etyp1 == NIL || (exp0->value_type & etyp1) == etyp1) &&
111111 (exp1 == NULL && etyp2 == NIL || (exp1->value_type & etyp2) == etyp2) &&
112- (exp2 == NULL && etyp2 == NIL || (exp2->value_type & etyp3) == etyp3) &&
113- (!(must_leftval & 1) || (exp0 != NULL && exp0->is_leftval)) &&
114- (!(must_leftval & 2) || (exp1 != NULL && exp1->is_leftval)) &&
115- (!(must_leftval & 4) || (exp2 != NULL && exp2->is_leftval))) {
112+ (exp2 == NULL && etyp3 == NIL || (exp2->value_type & etyp3) == etyp3)) {
113+
114+ /*
115+ // (!(must_leftval & 1) || (exp0 != NULL && exp0->is_leftval)) &&
116+ // (!(must_leftval & 2) || (exp1 != NULL && exp1->is_leftval)) &&
117+ // (!(must_leftval & 4) || (exp2 != NULL && exp2->is_leftval)))
118+
119+ 上のように書くと実行時エラーになってしまう。( && 演算子の簡略評価がなされないみたい)
120+ どうすれば下の if( ... ) continue; 連は簡単にできるのだろうか?
121+ */
122+
123+ // 式が左辺値であることを確認
124+
125+ if (must_leftval & 1 && !exp0) continue;
126+ if (must_leftval & 2 && !exp1) continue;
127+ if (must_leftval & 4 && !exp2) continue;
128+ if (must_leftval & 1 && !exp0->is_leftval) continue;
129+ if (must_leftval & 2 && !exp1->is_leftval) continue;
130+ if (must_leftval & 4 && !exp2->is_leftval) continue;
131+
132+ // ここからが本題の処理。
116133
117134 exp = alloc_plain_expression();
118135 exp->exp[0] = exp0;
@@ -406,6 +423,9 @@ expression *evaluate_term(input_stream *stream, asmi_context *context, int level
406423 rexp = generate_conv_expression(exp2, exp->value.type_value);
407424
408425 }
426+ else {
427+ rexp = exp;
428+ }
409429
410430 }
411431
@@ -457,7 +477,7 @@ void debug_expression(expression *exp, int indent) {
457477
458478 int i;
459479 for (i = 0; i < indent; i++)printf("\t");
460- printf("%s", get_op_string(exp->id));
480+ printf("%s\n", get_op_string(exp->id));
461481 if (exp->exp[0] != NULL) debug_expression(exp->exp[0], indent + 1);
462482 if (exp->exp[1] != NULL) debug_expression(exp->exp[1], indent + 1);
463483 if (exp->exp[2] != NULL) debug_expression(exp->exp[2], indent + 1);
--- a/asmi/parser.c
+++ b/asmi/parser.c
@@ -33,6 +33,7 @@ void parse_compound_statement(input_stream *stream, asmi_context *context, int l
3333 }
3434 else {
3535 // 式として評価
36+ push_token(tkn, context);
3637 expression *expr = evaluate_expression(stream, context);
3738
3839 debug_expression(expr, 0);
--- a/asmi/source.txt
+++ b/asmi/source.txt
@@ -1,6 +1,3 @@
11 
2-we
3-#include "source2.txt"
4-we
5-we
6-wewe
2+
3+123 * 234 / 3232 * 1234 / 234.5
--- a/asmi/source2.txt
+++ b/asmi/source2.txt
@@ -1,4 +0,0 @@
1-
2-3
3-aiueo
4-0.12345
\ No newline at end of file
--- a/asmi/token.c
+++ b/asmi/token.c
@@ -325,6 +325,7 @@ BOOL read_operator2(char *c, token *tkn, input_stream *stream, asmi_context *con
325325 }
326326 tkn->id = symbol_word_table[i].id;
327327 tkn->kind = symbol_word_table[i].kind;
328+ ret = TRUE;
328329 *(++op) = *c = next_char(stream, FALSE, context);
329330 if (*c == EOF) break;
330331 }
旧リポジトリブラウザで表示