void constants_add( int code, TYPEDEF type, int value ) { CONSTANT * c; if ( constants_used == constants_reserved ) constants_alloc( 16 ) ; if ( varspace_search( &global, code ) ) { token.code = code; token.type = IDENTIFIER; compile_error( MSG_VARIABLE_REDECLARED_AS_CONSTANT ) ; } if ( varspace_search( &local, code ) ) { token.code = code; token.type = IDENTIFIER; compile_error( MSG_VARIABLE_REDECLARED_AS_CONSTANT ) ; } if (( c = constants_search( code ) ) && ( !typedef_is_equal( c->type, type ) || c->value != value ) ) { token.code = string_new( identifier_name( code ) ); token.type = STRING; compile_error( "Constant redefined" ); } constants[constants_used].code = code ; constants[constants_used].type = type ; constants[constants_used].value = value ; constants_used++ ; }
void *compile(const std::string code){ static int count = 0; std::string cpp_file_name, o_file_name, so_file_name, clang_command_line; ++count; std::string count_str = lexical_cast(count); cpp_file_name = "cmpxx-temp-" + count_str + ".cpp"; o_file_name = "cmpxx-temp-" + count_str + ".o"; so_file_name = "./cmpxx-temp-" + count_str + CMPXX_DLEXT; { std::ofstream ofile(cpp_file_name.c_str()); if(ofile.fail()){ throw(ofile_error("cannot open file '" + cpp_file_name + "'.")); } ofile << code; } int status; clang_command_line = "clang++ -std=c++11 -c -O2 " + cpp_file_name; status = std::system(clang_command_line.c_str()); if(status != 0){ throw(compile_error("compile failed. '" + clang_command_line + "'.")); } clang_command_line = "clang++ -shared -o " + so_file_name + " " + o_file_name + " -lgmpxx -lgmp"; status = std::system(clang_command_line.c_str()); if(status != 0){ throw(compile_error("compile failed. '" + clang_command_line + "'.")); } void *handle = dlopen(so_file_name.c_str(), RTLD_LAZY); if(!handle){ throw(compile_error("dlopen failed. '" + so_file_name + "'.")); } return handle; }
int typedef_subsize( TYPEDEF t, int c ) { switch ( t.chunk[c].type ) { case TYPE_BYTE: case TYPE_SBYTE: case TYPE_CHAR: return 1 ; case TYPE_WORD: case TYPE_SHORT: return 2 ; case TYPE_DWORD: case TYPE_INT: case TYPE_FLOAT: case TYPE_STRING: case TYPE_POINTER: return 4 ; case TYPE_ARRAY: return t.chunk[c].count * typedef_subsize( t, c + 1 ) ; case TYPE_STRUCT: return t.varspace->size ; default: compile_error( MSG_INCOMP_TYPE ) ; return 0 ; } }
extern void handle_command(void) { int cmdtok; get_token(); cmdtok = match_tok(cmd_tab, TABSIZE(cmd_tab)); if (cmdtok < 0 || cmdtok >= (int)(sizeof(cmd_funcs) / sizeof(cmd_fn))) { file.lpos += strlen(buffer); compile_error_skip(-/*Unknown command “%s”*/12, buffer); return; } switch (cmdtok) { case CMD_EXPORT: if (!f_export_ok) compile_error(/**EXPORT must immediately follow “*BEGIN <SURVEY>”*/57); break; case CMD_COPYRIGHT: case CMD_DATE: case CMD_INSTRUMENT: case CMD_TEAM: case CMD_TITLE: /* These can occur between *begin and *export */ break; default: /* NB: additional handling for "*begin <survey>" in cmd_begin */ f_export_ok = fFalse; break; } cmd_funcs[cmdtok](); }
static void report_missing_export(prefix *pfx, int depth) { const char *filename_store = file.filename; unsigned int line_store = file.line; prefix *survey = pfx; char *s; int i; for (i = depth + 1; i; i--) { survey = survey->up; SVX_ASSERT(survey); } s = osstrdup(sprint_prefix(survey)); if (survey->filename) { file.filename = survey->filename; file.line = survey->line; } compile_error(/*Station “%s” not exported from survey “%s”*/26, sprint_prefix(pfx), s); if (survey->filename) { file.filename = filename_store; file.line = line_store; } osfree(s); }
void Pattern::compile (void) { // Compile the pattern int offset; const char * error; _re = pcre_compile (_pattern.c_str(), 0, &error, &offset, NULL); if (_re == NULL) { std::string offsetStr; std::stringstream ss; ss << offset; offsetStr = ss.str(); std::string msg ("PCRE compiling failed at offset " + offsetStr); throw compile_error (msg); } // Allocate an appropriate amount // of memory for the output vector. int captureCount; pcre_fullinfo (_re, NULL, PCRE_INFO_CAPTURECOUNT, &captureCount); delete[] _ovector; _ovector = new int[ (captureCount + 1) *3]; _ovectorSize = (captureCount + 1) * 3; }
VARSPACE * varspace_new() { VARSPACE * v = ( VARSPACE * ) calloc( 1, sizeof( VARSPACE ) ) ; if ( !v ) compile_error( "varspace_new: out of memory\n" ) ; varspace_init( v ) ; return v ; }
void include_file( int bprepro ) { static char buffer[1024]; char * buffer_ptr = buffer; int actual_line = line_count; SKIP_SPACES_UNTIL_LF_AND_COUNT_LINES; if ( *source_ptr == '"' ) { source_ptr++; buffer_ptr = buffer; while ( *source_ptr && *source_ptr != '"' ) { if ( buffer_ptr == buffer + 1023 ) compile_error( MSG_IDENTIFIER_TOO_LONG ); *buffer_ptr++ = *source_ptr++; } if ( *source_ptr == '"' ) source_ptr++; *buffer_ptr = 0; if ( bprepro ) { SKIP_SPACES_UNTIL_LF_AND_COUNT_LINES; if ( *source_ptr == '\n' ) line_count--; } else { SKIP_SPACES; } if ( *source_ptr == ';' ) { if ( bprepro ) { compile_warning( "extra tokens at end of #include directive" ); SKIP_ALL_UNTIL_LF_AND_COUNT_LINES; if ( *source_ptr == '\n' ) line_count--; } else token_next(); } load_file( buffer ); token_next(); return; } line_count = actual_line; compile_error( MSG_FILENAME_EXP ); }
static void out_of_mem() { static char out[] = "out of memory" ; /* I don't think this will ever happen */ compile_error(out) ; exit(2) ; }
/* * Field attributes are used to specify the size of an array, or the portion * of the array, that contains valid data, which is done by associating * another parameter with the array that contains the sizing information. * * Supports formats such as size_is(x) or size_is(x / 2). The supported * operators are: * * * / % + - & | ^ */ void print_field_attr(ndr_node_t *np) { static char *valid = "*/%+-&|^"; ndr_node_t *arg; char *name; char *operator; long value; arg = np->n_a_arg; if (arg->label != IDENTIFIER) fatal_error("invalid label %d", arg->label); if ((name = arg->n_sym->name) == NULL) fatal_error("missing symbol name"); arg = np->n_a_arg1; operator = NULL; if (arg->label == IDENTIFIER) { operator = arg->n_sym->name; if (operator != NULL) { /* * The lexer sets the name and operator to * the same value if there is no operator. */ if (strcmp(name, operator) == 0) operator = NULL; else if (strchr(valid, *operator) == NULL) compile_error("invalid operator: %s", operator); } } arg = np->n_a_arg2; if (arg->label == INTEGER) { value = arg->n_int; if ((value == 0) && strcmp(operator, "/") == 0) compile_error("divide by zero"); } if (operator) (void) printf("%s %s %ldUL", name, operator, value); else (void) printf("%s", name); }
static void cmd_flags(void) { static sztok flagtab[] = { {"DUPLICATE", FLAGS_DUPLICATE }, {"NOT", FLAGS_NOT }, {"SPLAY", FLAGS_SPLAY }, {"SURFACE", FLAGS_SURFACE }, {NULL, FLAGS_UNKNOWN } }; bool fNot = fFalse; bool fEmpty = fTrue; while (1) { int flag; get_token(); /* If buffer is empty, it could mean end of line, or maybe * some non-letter junk which is better reported later */ if (!buffer[0]) break; fEmpty = fFalse; flag = match_tok(flagtab, TABSIZE(flagtab)); /* treat the second NOT in "NOT NOT" as an unknown flag */ if (flag == FLAGS_UNKNOWN || (fNot && flag == FLAGS_NOT)) { file.lpos += strlen(buffer); compile_error(-/*FLAG “%s” unknown*/68, buffer); /* Recover from “*FLAGS NOT BOGUS SURFACE” by ignoring "NOT BOGUS" */ fNot = fFalse; } else if (flag == FLAGS_NOT) { fNot = fTrue; } else if (fNot) { pcs->flags &= ~BIT(flag); fNot = fFalse; } else { pcs->flags |= BIT(flag); } } if (fNot) { file.lpos += strlen(buffer); compile_error(-/*Expecting “DUPLICATE”, “SPLAY”, or “SURFACE”*/188); } else if (fEmpty) { file.lpos += strlen(buffer); compile_error(-/*Expecting “NOT”, “DUPLICATE”, “SPLAY”, or “SURFACE”*/189); } }
void varspace_varstring( VARSPACE * n, int offset ) { if ( n->stringvar_reserved == n->stringvar_count ) { n->stringvars = ( int * ) realloc( n->stringvars, ( n->stringvar_reserved += 16 ) * sizeof( int ) ) ; if ( !n->stringvars ) compile_error( "varspace_varstring: out of memory\n" ) ; } n->stringvars[n->stringvar_count++] = offset ; }
/* checkGetTokenの, 2種類対応版. * kind1, またはkind2との一致を確認して, 次のトークンを返す */ Token checkGetToken2(Token token, Token_kind kind1, Token_kind kind2) { if (token.kind == kind1 || token.kind == kind2) { /* 一致していれば次のトークンを返す */ return nextToken(); } else { /* 一致していなければエラー */ if (token.kind == OTHERS) { /* 無効なトークン */ compile_error(INVAILD_CHAR_ERROR, line_number); } /* シンタックスエラー */ compile_error(SYNTAX_ERROR, line_number); } /* unreachable here */ exit(1); }
static int arg_cnt_ok(FBLOCK * fbp, CA_REC * q) { if ((int) q->arg_num >= (int) fbp->nargs) { compile_error("too many arguments in call to %s", fbp->name); return 0; } else return 1; }
void varspace_init( VARSPACE * n ) { n->vars = ( VARIABLE * ) calloc( 16, sizeof( VARIABLE ) ) ; n->reserved = 16 ; n->count = 0 ; n->size = 0 ; n->stringvars = 0 ; n->stringvar_reserved = 0 ; n->stringvar_count = 0 ; if ( !n->vars ) compile_error( "varspace_init: out of memory\n" ) ; }
static void out_of_mem() { static char out[] = "out of memory" ; if (mawk_state == EXECUTION) rt_error(out) ; else { /* I don't think this will ever happen */ compile_error(out) ; mawk_exit(2) ; } }
extern void read_string(char **pstr, int *plen) { s_zero(pstr); skipblanks(); if (ch == '\"') { /* String quoted in "" */ nextch(); while (1) { if (isEol(ch)) { compile_error(-/*Missing \"*/69); LONGJMP(file.jbSkipLine); } if (ch == '\"') break; s_catchar(pstr, plen, ch); nextch(); } } else { /* Unquoted string */ while (1) { if (isEol(ch) || isComm(ch)) { if (!*pstr || !(*pstr)[0]) { compile_error(-/*Expecting string field*/121); LONGJMP(file.jbSkipLine); } return; } if (isBlank(ch)) break; s_catchar(pstr, plen, ch); nextch(); } } nextch(); }
void typedef_name( TYPEDEF t, int code ) { if ( named_count >= named_reserved ) { named_reserved += 16; named_types = ( TYPEDEF * ) realloc( named_types, named_reserved * sizeof( TYPEDEF ) ); named_codes = ( int * ) realloc( named_codes, named_reserved * sizeof( int ) ); if ( !named_types || !named_codes ) compile_error( "typedef_name: out of memory\n" ) ; } named_codes[named_count] = code ; named_types[named_count] = t ; named_count++ ; }
static int collect_string(void) { register char *p = string_buff; int c; int e_flag = 0; /* on if have an escape char */ size_t len_buff; while (1) { CheckStringSize(p); switch (scan_code[NextUChar(*p++)]) { case SC_DQUOTE: /* done */ *--p = 0; goto out; case SC_NL: p[-1] = 0; /* FALLTHRU */ case 0: /* unterminated string */ compile_error( "runaway string constant \"%.10s ...", string_buff); mawk_exit(2); case SC_ESCAPE: if ((c = next()) == '\n') { p--; lineno++; } else if (c == 0) un_next(); else { *p++ = (char) c; e_flag = 1; } break; default: break; } } out: if (e_flag) rm_escape(string_buff, &len_buff); else len_buff = (unsigned) ((char *) p - string_buff); yylval.ptr = (PTR) new_STRING1(string_buff, len_buff); return STRING_; }
int load_file( char * filename ) { long size; file * fp = file_open( filename, "rb0" ); char * source; if ( n_files == MAX_SOURCES ) compile_error( MSG_TOO_MANY_FILES ); strcpy( files[n_files++], filename ); if ( !fp ) compile_error( MSG_FILE_NOT_FOUND, filename ); size = file_size( fp ); source = ( char * ) calloc( size + 1, sizeof( char ) ); if ( !source ) compile_error( MSG_FILE_TOO_BIG, filename ); if ( size == 0 ) compile_error( MSG_FILE_EMPTY, filename ); if ( !file_read( fp, source, size ) ) compile_error( MSG_READ_ERROR, filename ); source[size] = 0; file_close( fp ); token_init( source, n_files - 1 ); return n_files -1; }
instruction break_() { // Follow implied terminators until a loopback is encountered. llvm::BasicBlock * bb = scope::current_label().ptr(); while(true) { // The Sprite branching constructs add implied continuations to every // successor in the CFG. If there is no terminator, then this must // have been called outside of any branch (e.g., in the function entry // block). llvm::TerminatorInst * term = bb->getTerminator(); if(!term) throw compile_error("break_ used outside of a loop(1)."); metadata md = instruction(term).get_metadata(SPRITE_IMPLIED_METADATA); if(!md.ptr()) throw compile_error("break_ used outside of a loop(2)."); else bb = term->getSuccessor(0); assert(bb); // If this is a loopback, then we've found the loop escape. The // loopback goes to the basic block that evaluates the condition // and the target of this break is the false branch of its // terminator. if(md.size() && valueof<int>(md[0]) == MD_LOOP) { term = bb->getTerminator(); assert(term); assert(term->getNumSuccessors() == 2); label target(term->getSuccessor(1)); return goto_(target); } } }
void token_init( const char * source, int file ) { char * ptr; char * clean_source; if ( sources == MAX_SOURCES ) compile_error( MSG_TOO_MANY_INCLUDES ); if ( !source ) { fprintf( stdout, "token_init: no source\n" ); exit( 1 ); } /* Perform cleaning of the source file */ clean_source = ( char * ) calloc( strlen( source ) + 2, sizeof( char ) ); ptr = clean_source; *ptr++ = '\n'; /* Adds a blank line to detect first-line # directives */ while ( *source ) { if ( *source == '\r' && source[1] == '\n' ) { source += 2; *ptr++ = '\n'; } else { *ptr++ = *source++; } } *ptr = 0; /* Store the old source pointer */ old_line_counts [sources] = line_count; old_current_file [sources] = current_file; old_sources [sources] = source_ptr; old_sources_start [sources] = source_start; sources++; /* Use the new source */ line_count = 0; current_file = file; source_ptr = clean_source; source_start = clean_source; use_saved = 0; }
int include(lua_State * L) { int n = lua_gettop(L); if( n != 1 ) return luaL_error(L, "Function requires a single parameter."); int type = lua_type(L, 1); if(type != LUA_TSTRING) return luaL_error(L, "Function requires parameter of type string."); const char * filename = lua_tostring(L, 1); // Get the file. love::pFile * file = filesystem->getFile(filename, love::FILE_READ); if(!(*file)->load()) { delete file; std::stringstream ss; ss << "Could not include file \"" << filename << "\"."; return luaL_error(L, ss.str().c_str()); } int status = luaL_loadbuffer (L, (const char *)(*file)->getData(), (*file)->getSize(), (*file)->getFilename().c_str()); delete file; if(status != 0) { compile_error(L, status); return 0; } // Stealing this from LuaGame. int narg = 0, nres = 0; int base = lua_gettop(L) - narg; /* function index */ lua_pushcfunction(L, runtime_error); /* push traceback function */ lua_insert(L, base); /* put it under chunk and args */ status = lua_pcall(L, narg, nres, base); lua_remove(L, base); /* remove traceback function */ /* force a complete garbage collection in case of errors */ if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0); return 0; }
static FCALL_REC * first_pass(FCALL_REC * p) { FCALL_REC dummy; register FCALL_REC *q = &dummy; /* trails p */ q->link = p; while (p) { if (!p->callee->code) { /* callee never defined */ compile_error("function %s never defined", p->callee->name); /* delete p from list */ q->link = p->link; /* don't worry about freeing memory, we'll exit soon */ } /* note p->arg_list starts with last argument */ else if (!p->arg_list /* nothing to do */ || (!p->arg_cnt_checked && !arg_cnt_ok(p->callee, p->arg_list))) { q->link = p->link; /* delete p */ /* the ! arg_list case is not an error so free memory */ ZFREE(p); } else { /* keep p and set call_start */ q = p; switch (p->call_scope) { case SCOPE_MAIN: p->call_start = main_start; break; case SCOPE_BEGIN: p->call_start = begin_start; break; case SCOPE_END: p->call_start = end_start; break; case SCOPE_FUNCT: p->call_start = p->call->code; break; } } p = q->link; } return dummy.link; }
static void cmd_export(void) { prefix *pfx; fExportUsed = fTrue; pfx = read_prefix(PFX_STATION); do { int depth = 0; { prefix *p = pfx; while (p != NULL && p != pcs->Prefix) { depth++; p = p->up; } /* Something like: *export \foo, but we've excluded use of root */ SVX_ASSERT(p); } /* *export \ or similar bogus stuff */ SVX_ASSERT(depth); #if 0 printf("C min %d max %d depth %d pfx %s\n", pfx->min_export, pfx->max_export, depth, sprint_prefix(pfx)); #endif if (pfx->min_export == 0) { /* not encountered *export for this name before */ if (pfx->max_export > depth) report_missing_export(pfx, depth); pfx->min_export = pfx->max_export = depth; } else if (pfx->min_export != USHRT_MAX) { /* FIXME: what to do if a station is marked for inferred exports * but is then explicitly exported? Currently we just ignore the * explicit export... */ if (pfx->min_export - 1 > depth) { report_missing_export(pfx, depth); } else if (pfx->min_export - 1 < depth) { compile_error(/*Station “%s” already exported*/66, sprint_prefix(pfx)); } pfx->min_export = depth; } pfx = read_prefix(PFX_STATION|PFX_OPT); } while (pfx); }
void add_simple_define( char * macro, char *text ) { int code = identifier_search_or_add( macro ); if ( find_define( code ) != -1 ) compile_error( MSG_MACRO_ERROR, identifier_name( code ) ); /* Allocate the macro */ if ( defines_allocated == defines_count ) { defines_allocated += 8; defines = ( DEFINE * ) realloc( defines, sizeof( DEFINE ) * defines_allocated ); } defines[defines_count].param_count = -1; defines[defines_count].code = code; defines[defines_count].text = strdup( text ); defines_count++; }
static void cmd_date(void) { int year, month, day; int days1, days2; bool implicit_range = fFalse; read_date(&year, &month, &day); days1 = days_since_1900(year, month ? month : 1, day ? day : 1); if (days1 > current_days_since_1900) { compile_warning(-/*Date is in the future!*/80); } skipblanks(); if (ch == '-') { nextch(); read_date(&year, &month, &day); } else { if (month && day) { days2 = days1; goto read; } implicit_range = fTrue; } if (month == 0) month = 12; if (day == 0) day = last_day(year, month); days2 = days_since_1900(year, month, day); if (!implicit_range && days2 > current_days_since_1900) { compile_warning(-/*Date is in the future!*/80); } if (days2 < days1) { compile_error(-/*End of date range is before the start*/81); } read: copy_on_write_meta(pcs); pcs->meta->days1 = days1; pcs->meta->days2 = days2; }
vm& run(::std::basic_ifstream< ::SQChar>& ifs, const ::std::basic_string< ::SQChar>& symbol = _SC("")) throw(compile_error, runtime_error) { if (SQ_FAILED(::sq_compile(this->hvm, detail::ifstream_lexfeed, reinterpret_cast< ::SQUserPointer>(&ifs), reinterpret_cast<const ::SQChar*>(symbol.c_str()), SQTrue))) { throw compile_error(); } ::sq_push(this->hvm, -2); if (SQ_FAILED(::sq_call(this->hvm, 1, SQFalse, SQTrue))) { ::sq_pop(this->hvm, 1); throw runtime_error(); } ::sq_remove(this->hvm, -1); return *this; }
vm& run(const ::std::basic_string< ::SQChar>& str, const ::std::basic_string< ::SQChar>& symbol = _SC("")) throw(compile_error, runtime_error) { if (SQ_FAILED(::sq_compilebuffer(this->hvm, reinterpret_cast<const ::SQChar*>(str.c_str()), static_cast< ::SQInteger>(str.size() * sizeof(::SQChar)), reinterpret_cast<const ::SQChar*>(symbol.c_str()), SQTrue))) { throw compile_error(); } ::sq_push(this->hvm, -2); if (SQ_FAILED(::sq_call(this->hvm, 1, SQFalse, SQTrue))) { ::sq_pop(this->hvm, 1); throw runtime_error(); } ::sq_remove(this->hvm, -1); return *this; }
void BC_insert(int type, INST * address) { register BC *p; if (error_state) return; if (type && !bc_top) { compile_error("%s statement outside of loop", type == 'B' ? "break" : "continue"); return; } else { p = ZMALLOC(BC); p->type = type; p->source_offset = (int) (address - code_base); p->link = bc_top; bc_top = p; } }