keyword_expression(KurumiKeyword keyword) { switch (keyword) { case KurumiKeyword.IF: KurumiNumber stop_p = new KurumiNumber(-1); if_statement(stop_p); break; case KurumiKeyword.ELSE: case KurumiKeyword.ELIF: if (if_statement_count <= 0) { throw new Exception("Keyword 'else' or 'elif' without if statement."); } break; case KurumiKeyword.GOTO: next(); KurumiToken current = get_current_token(); expression.add(new KurumiCommand(KurumiCommandType.JMP)); expression.add(goto_storage.get(current.data)); next(); break; default: break; } }
if_statement(KurumiNumber stop_p) { next(); if_statement_count += 1; rexpr(); next(); KurumiNumber next_p = new KurumiNumber(-1); expression.add(new KurumiCommand(KurumiCommandType.JMPIN)); expression.add(next_p); bool additional_branching = false; KurumiToken current = get_current_token(); while (current != null) { KurumiKeyword kw = current.is_keyword(); if (kw == KurumiKeyword.END) { int end_position = expression.size(); stop_p.number = end_position; if (!additional_branching) { next_p.number = end_position; } break; } else if (kw == KurumiKeyword.ELSE) { additional_branching = true; expression.add(new KurumiCommand(KurumiCommandType.JMP)); expression.add(stop_p); next_p.number = expression.size(); next(); } else if (kw == KurumiKeyword.ELIF) { additional_branching = true; expression.add(new KurumiCommand(KurumiCommandType.JMP)); expression.add(stop_p); next_p.number = expression.size(); if_statement(stop_p); break; } else { lexpr(); } current = get_current_token(); } next(); if_statement_count -= 1; }
lexpr() { KurumiToken current = get_current_token(); switch (current.type) { case TokenType.Identifier: KurumiKeyword keyword = current.is_keyword(); if (keyword == KurumiKeyword.UNKNOWN) { KurumiToken n = get_current_token(1); if (n == null) { return; } OperatorType type = n.to_operator_type(); if (type == OperatorType.COLON) { KurumiNumber number = goto_storage.get(current.data); number.number = expression.size(); next(); next(); } else if (type == OperatorType.LBRACKET) { function_call(current, 0); } else { rexpr(); } } else { keyword_expression(keyword); } break; case TokenType.Operator: next(); return; default: throw new Exception("Identifier or Keyword is expected in the left-sided expression."); } }