static C_RESULT parse_string(FILE* f, char* str, int32_t maxlen) { int32_t i = 0; bool_t is_quoted = is_quote(current_c); if( is_quoted ) { while( SUCCEED(fetch_char(f)) && ! ( is_separator(current_c) && is_quote(current_c) ) ) { str[i] = current_c; i++; } } else { while( SUCCEED(fetch_char(f)) && !is_separator(current_c) ) { str[i] = current_c; i++; } } str[i] = '\0'; // PRINT("parse_string: %s\n", str); return is_eol( current_c ) ? C_FAIL : C_OK; }
static obj_t make_string_fn(obj_t args, Reporter rep) { size_t nargs, i, k; char ch; string str; nargs = list_length(args); if (nargs == 0 || nargs > 2) { reportf(rep, "make-string: " "length and optional fill char expected"); return unspecific; } if (!is_num(list_ref(args, 0))) { reportf(rep, "make-string: " "first argument must be a non-negative integer"); return unspecific; } if (nargs == 2 && !is_char(list_ref(args, 1))) { reportf(rep, "make-string: " "second argument must be a character"); return unspecific; } k = fetch_num(list_ref(args, 0)); ch = (nargs == 1) ? 0 : fetch_char(list_ref(args, 1)); str = string_alloc(k); for (i = 0; i < str->len; i++) str->data[i] = ch; return make_string(str); }
static obj_t string_fn(obj_t args, Reporter rep) { string str; size_t i; if (!args_plus_match(rep, args, 0, is_char)) return unspecific; str = string_alloc(list_length(args)); for (i = 0; !is_null(args); i++, args = cdr(args)) str->data[i] = fetch_char(car(args)); return make_string(str); }
static obj_t string_fill_fn(obj_t args, Reporter rep) { string str; char ch; if (!args_match(rep, args, 2, is_string, is_char)) return unspecific; str = fetch_string(list_ref(args, 0)); ch = fetch_char(list_ref(args, 1)); memset(str->data, ch, str->len); return unspecific; }
static C_RESULT parse_I(FILE* f) { char keyword[KW_MAX_LEN]; while( SUCCEED(fetch_char(f)) && is_separator(next_c) ); while( !is_eol(next_c) ) { keyword_t kw; parse_string( f, keyword, KW_MAX_LEN ); if( SUCCEED( match_keyword( keyword, &kw ) ) ) { parse_keyword( f, kw ); } } return C_OK; }
static obj_t string_set_fn(obj_t args, Reporter rep) { obj_t obj, idx, chr; long k; if (!args_match(rep, args, 3, is_string, is_num, is_char)) return unspecific; obj = list_ref(args, 0); idx = list_ref(args, 1); chr = list_ref(args, 2); k = fetch_num(idx); if (k < 0 || (size_t)k >= fetch_string(obj)->len) { reportf(rep, "string-set!: index %ld is out of range", k); return unspecific; } fetch_string(obj)->data[k] = fetch_char(chr); return unspecific; }
void main(void) { /* put your own code here */ EnableInterrupts; DDR1AD0 = DDRT = 0xFF; PT1AD0 = PTT = 0x00; set_baud_rate(9600); send(); fct_set(8,2,58); disp_on(1,1,1); entry_mode(1,0); clear_disp(); char val,buf[16] ; #define go 5000 while(1) { memset(buf,0,16); for(int v=0,i =0; i<16 ; i++) { v=0; while( chk_rx() >= 0 ? 0: i==0 ? 1: v++ < go ? 1: 0); if (v >= go){ break; } buf[i]=fetch_char(); } return_home(); clear_disp(); prt_str(buf); } for(;;) { _FEED_COP(); /* feeds the dog */ } /* loop forever */ /* please make sure that you never leave main */ }
static C_RESULT parse_H(FILE* f) { C_RESULT res = C_FAIL; char keyword[KW_MAX_LEN]; while( SUCCEED(fetch_char(f)) && is_separator(next_c) ); while( !is_eol(next_c) ) { parse_string( f, keyword, KW_MAX_LEN ); if( strcmp( keyword, kw_tab[KW_HANDLERS].name ) == 0 ) { while( FAILED(res) && SUCCEED( parse_string(f, (char*)((char*)¤t_device + kw_tab[KW_HANDLERS].value_offset), KW_MAX_LEN ) ) ) { res = match_handler(); } } } return res; }
static C_RESULT parse_int(FILE* f, int32_t* i) { C_RESULT res = C_OK; int value; *i = 0; while( !is_separator(next_c) && SUCCEED(fetch_char(f)) && res == C_OK ) { value = current_c - '0'; if (value > 9 || value < 0) { value = current_c - 'a' + 10; res = (value > 0xF || value < 0xa) ? C_FAIL : C_OK; } *i *= 16; *i += value; } return res; }
static void print_atom(FILE *out, obj_t obj) { if (eq(obj, unspecific)) { fputs("#<unspecified>", out); } else if (is_null(obj)) { fputs("()", out); } else if (is_bool(obj)) { if (fetch_bool(obj)) fputs("#t", out); else fputs("#f", out); } else if (is_symbol(obj)) { print_string(out, fetch_symbol(obj)); } else if (is_num(obj)) { fprintf(out, "%ld", (long)fetch_num(obj)); } else if (is_char(obj)) { char ch = fetch_char(obj); switch (ch) { case ' ': fputs("#\\space", out); break; case '\n': fputs("#\\newline", out); break; default: fprintf(out, "#\\%c", ch); break; } } else if (is_string(obj)) { putc('"', out); print_string(out, fetch_string(obj)); putc('"', out); } else if (is_pair(obj)) { if (!print_quotation(out, obj)) print_list(out, obj); } else { fputs("#<unknown>", out); /* TODO: function, lambda */ } }
static C_RESULT parse_proc_input_devices(FILE* f, const int32_t id) { C_RESULT res = C_FAIL; next_c = '\0'; vp_os_memset( ¤t_device, 0, sizeof(device_t) ); while( res != C_OK && SUCCEED( fetch_char(f) ) ) { switch( next_c ) { case 'I': parse_I(f); break; case 'N': parse_N(f); break; case 'H': if( SUCCEED( parse_H(f) ) ) res = end_device(id); break; case 'P': case 'S': case 'B': default: skip_line(f); break; } } return res; }
static C_RESULT parse_keyword( FILE* f, keyword_t kw ) { C_RESULT res = C_OK; while( is_separator(next_c) && SUCCEED(fetch_char(f)) ); switch( kw_tab[kw].value_type ) { case INT: parse_int( f, (int32_t*)((char*)¤t_device + kw_tab[kw].value_offset) ); //PRINT("%s = %x\n", kw_tab[kw].name, *(int32_t*)((char*)¤t_device + kw_tab[kw].value_offset) ); break; case STRING: parse_string( f, (char*)((char*)¤t_device + kw_tab[kw].value_offset), KW_MAX_LEN ); //PRINT("%s = %s\n", kw_tab[kw].name, (char*)((char*)¤t_device + kw_tab[kw].value_offset) ); break; default: res = C_FAIL; break; } return res; }
int loaddefs(int fromfile, char *fname, int mode) { nialptr ts; int repeatloop, keepreading, nolines, inremark, linecnt; FILE *f1 = NULL; /* initialized to avoid complaint */ int errorsfound; if (fromfile) { f1 = openfile(fname, 'r', 't'); if (f1 == OPENFAILED) return (false); pushsysfile(f1); } /* a loaddefs always affects the global environment. We reset current_env to relect this. The code to restore the environment is below. This must be saved on the stack, otherwise it can get thrown away since it may only be owned by a transient definition value. The following example failed before I protected this on the stack: retry is { host 'vi bug.ndf'; loaddefs"bug l } where this definition was in the file bug.ndf. */ apush(current_env); current_env = Null; ts = topstack; /* to monitor stack growth on each action */ errorsfound = 0; /* reset parse error counter */ repeatloop = true; linecnt = 0; /* loop to pick up groups of lines */ while (repeatloop) { /* continue as long as their are line groups */ /* test on each circuit if an interrupt signal has been posted */ #ifdef USER_BREAK_FLAG if (fromfile) checksignal(NC_CS_NORMAL); #endif inremark = false; nolines = 0; keepreading = true; /* loop to pick up lines until a whitespace line occurs */ while (keepreading) { if (fromfile) { /* reading a line from the file */ readfileline(f1, (mode ? 2 : 0)); /* mode==2 only in a loaddefs */ /* readfileline places result on the stack */ if (top == Eoffault) { apop(); /* to remove the end of file marker */ repeatloop = false; break; /* to end read loop */ } } else { /* select a line from array defsndf loadded from defstbl.h */ char *line; line = defsndf[linecnt++]; if (linecnt == NOLINES) { repeatloop = false; keepreading = false; /* to end read loop */ } mkstring(line); /* convert the line to a Nial string and push it */ } if (nolines == 0) { /* check first line of group for a remark */ char firstchar; int i = 0; /* loop to skip blanks */ while (i < tally(top) && fetch_char(top, i) <= BLANK) i++; /* note whether first char is "#" */ firstchar = fetch_char(top, i); if (tally(top)) inremark = firstchar == HASHSYMBOL; else inremark = false; } /* if the line is all while space then we are at the end of a group */ if (top == Null || allwhitespace(pfirstchar(top))) { keepreading = false; freeup(apop()); /* to get rid of the empty line */ } else /* count the line on the stack */ nolines++; } /* we have a group of lines to process */ if (nolines > 0) { mklist(nolines); /* create a list of lines and link them*/ ilink(); if (inremark) { freeup(apop()); /* remarks are ignored */ } else { /* carry out the actions of the main loop */ iscan(); parse(true); /* check whether parse produced an error */ if (kind(top) == faulttype) { if (top != Nullexpr) { errorsfound++; if (mode == 0) { /* show error message */ apush(top); ipicture(); show(apop()); } } } /* evaluate the parse tree, if it is a fault, it is the value returned */ ieval(); #ifdef DEBUG memchk(); #endif if (mode) { /* show the result */ if (top != Nullexpr) { ipicture(); show(apop()); } else apop(); /* the Nullexpr */ } else freeup(apop()); /* free because it might not be Nullexpr */ } if (mode) { /* now display empty line */ writechars(STDOUT, "", (nialint) 0, true); if (keeplog && f1 == STDIN) writelog("", 0, true); } } /* check that the stack hasn't grown */ if (ts != topstack) { while (ts != topstack) freeup(apop()); exit_cover(NC_STACK_GROWN_I); } } /* done reading groups of lines */ if (fromfile) { closefile(f1); popsysfile(); } /* restore the current_env */ current_env = apop(); if (errorsfound > 0) nprintf(OF_NORMAL_LOG, "errors found: %d\n", errorsfound); return (true); }
static C_RESULT skip_line(FILE* f) { while( !is_eol(next_c) && SUCCEED(fetch_char(f)) ); return C_OK; }