inline stmt_def_field(const statement&parent,const token&tk,tokenizer&t): statement{parent,tk}, ident_{t.next_token()} { if(ident_.is_name("")) throw compiler_error(ident_,"expected field name"); if(!t.is_next_char('{')) throw compiler_error(ident_,"expected '{' initial value then '}' ",ident_.name()); while(true){ if(t.is_next_char('}'))break; tokens_.push_back(t.next_token()); } }
void parent(pid_t pid, int* cmd_stdout, int* cmd_stderr) { /* We dont need input descriptors, we'll be only reading from the child */ close(cmd_stderr[1]); close(cmd_stdout[1]); string buf = read_all(cmd_stderr[0]); buf += read_all(cmd_stdout[0]); /* We've read it all, not needed any more */ close(cmd_stderr[0]); close(cmd_stdout[0]); /* Clean up child's status */ int status; if(waitpid(pid, &status, 0/*options*/) == -1) throw system_error("waitpid", errno); #ifdef REPORT_COMPILER_EXIT_STATUS /* Compiler process reported errors - throw exception */ stringstream exit_info; if(WIFSIGNALED(status)) { exit_info << "Compiler exited due to signal "<< WTERMSIG(status) << endl; }else if(WIFEXITED(status) && WEXITSTATUS(status) != 0){ exit_info << "Compiler exited normaly with status " << WEXITSTATUS(status) << endl; } buf.insert(0, exit_info.str() + "\n"); #endif if(!buf.empty()) { throw compiler_error(buf); } }
void statement_code_generator::visit(continue_tree const &statement) { if (auto * const loop = m_frame.get_loop()) { loop->emit_continue(); } else { throw compiler_error("Found continue statement outside of a loop", statement.position()); } }
inline stmt_def_func_param(const statement&parent,tokenizer&t): statement{parent,t.next_token()} { assert(!tok().is_name("")); if(!t.is_next_char(':')) return; while(true){ if(t.is_eos())throw compiler_error(*this,"unexpected end of stream",tok().name_copy()); keywords_.push_back(t.next_token()); if(t.is_next_char(':')) continue; break; } }
/** * * * @param void * @return */ bool add_var_def(void) { bool status = false; if(var_cnt < MAX_VARIABLES) { strncpy((char*)(var_def[var_cnt].name), (char const*) g_var_name, MAX_CHARS_VAR_NAME); var_def[var_cnt].addr = XVM_U32_MAX_RAM + var_cnt; var_cnt++; status = true; } else { printf("ERROR: too many variable definitions.\n"); compiler_error(); } return status; }
/* 5.4: Case statement */ Tuple make_case_table(Node cases_node) /*;make_case_table*/ { /* Function : takes a set of alternatives, and produces a linear table * suitable for jump table, of case ranges sorted in ascending * order. Some optimisation is done, to merge contiguous * ranges and to fill missing ranges with "others" case * Input : case_node ::= {case_statements} * case_statements ::= [choice_list, body] * choice_list ::= { choice } * choice ::= simple_choice | range_choice * | others_choice * simple_choice ::= [ value ] * range_choice ::= [ subtype ] * Output : [table, bodies, others_body] * table ::= [ [ lower_bound, index ] ] * - an extra pair is added with a "lower_bound" one step * higher than necessary * - "index" is an index in the tuple "bodies", and * index = 0 means "others" */ Node case_statements_node, choice_list_node, body_node, choice_node, lbd_node, ubd_node, others_body; Tuple result, tup, bodies, triplets; int index, a1, a2, a3, b1, b2, b3, lbd_int, ubd_int; int empty; Fortup ft1, ft2; #ifdef TRACE if (debug_flag) gen_trace_node("MAKE_CASE_TABLE", cases_node); #endif /* 1. build a set of triples [lowerbound, upperbound, index] */ index = 0; bodies = tup_new(0); triplets = tup_new(0); others_body = OPT_NODE; FORTUP(case_statements_node = (Node), N_LIST(cases_node), ft1); choice_list_node = N_AST1(case_statements_node); body_node = N_AST2(case_statements_node); index += 1; empty = TRUE; /* may be we have an empty branch */ FORTUP(choice_node = (Node), N_LIST(choice_list_node), ft2); switch (N_KIND(choice_node)) { case (as_range): lbd_node = N_AST1(choice_node); ubd_node = N_AST2(choice_node); lbd_int = get_ivalue_int(lbd_node); ubd_int = get_ivalue_int(ubd_node); if (lbd_int <= ubd_int) { tup = tup_new(3); tup[1] = (char *) lbd_int; tup[2] = (char *) ubd_int; tup[3] = (char *) index; triplets = tup_with(triplets, (char *) tup); empty = FALSE; } break; case (as_others_choice): others_body = body_node; break; default: compiler_error( "Unknown kind of choice: "); } ENDFORTUP(ft2); if (empty) index -= 1; else bodies = tup_with(bodies, (char *) body_node); ENDFORTUP(ft1); result = tup_new(0); if (tup_size(triplets) != 0) { /* We may have a completely empty case */ /* 2. sort the set of triples, giving a tuple */ triplets = sort_case(triplets); /* 3. build the case table, filling gaps and merging adjacent cases */ tup = (Tuple) tup_fromb(triplets); a1 = (int) tup[1]; a2 = (int) tup[2]; a3 = (int) tup[3]; while(tup_size(triplets) != 0) { tup = (Tuple) tup_fromb(triplets); b1 = (int) tup[1]; b2 = (int) tup[2]; b3 = (int) tup[3]; if (a2 != b1-1) { /* gap */ tup = tup_new(2); tup[1] = (char *) a1; tup[2] = (char *) a3; result = tup_with(result, (char *) tup); tup = tup_new(2); tup[1] = (char *) (a2+1); tup[2] = (char *) 0; result = tup_with(result, (char *) tup); a1 = b1; a2 = b2; a3 = b3; } else if (a3 == b3) { /* merge */ a2 = b2; a3 = b3; } else { tup = tup_new(2); tup[1] = (char *) a1; tup[2] = (char *) a3; result = tup_with(result, (char *) tup); a1 = b1; a2 = b2; a3 = b3; } } tup = tup_new(2); tup[1] = (char *) a1; tup[2] = (char *) a3; result = tup_with(result, (char *) tup); tup = tup_new(2); if (a2 != MAX_INTEGER) { tup[1] = (char *) a2+1; tup[2] = (char *) 0; } else { tup[1] = (char *) 0; /* does not really matter */ tup[2] = (char *) a3;/* merge with the preceeding */ } result = tup_with(result, (char *) tup); } tup = tup_new(3); tup[1] = (char *) result; tup[2] = (char *) bodies; tup[3] = (char *) others_body; return tup; }