OBJ builtin_include(int numArgs){ if( numArgs != 1){ js_error("(include): expects 1 argument", js_nil); } OBJ arg1 = POP(); char *fileName; FILE *theFile; if( !ISSTRING(arg1) ){ js_error("(include): string arg expected:", arg1); } fileName = STRINGVAL(arg1); theFile = fopen(fileName, "r"); if( theFile == NULL){ js_error("(include): cannot open file:", arg1); } OBJ inputStream = newFileStream(theFile); #ifdef DEBUG if( PRINT_INCLUDE->state){ printIndent(indentLevelForInclude); fprintf(stdout, "<include file:" YEL " %s" RESET" >\n", fileName); indentLevelForInclude++; } if( CONTINUATION_PASSING->state){ enterTrampoline1(inputStream); } else{ OBJ expr = js_read(inputStream); OBJ result; while( expr != js_eof){ result = js_eval(globalEnvironment, expr); if(PRINT_INCLUDE->state){ printIndent(indentLevelForInclude); js_print(stdout, result,1); printf("\n"); } expr = js_read(inputStream); } } if( PRINT_INCLUDE->state){ indentLevelForInclude--; printIndent(indentLevelForInclude); fprintf(stdout, "<EOF:" YEL " %s" RESET" >\n", fileName); } #else enterTrampoline1(inputStream); #endif fclose( inputStream->u.fileStream.file ); free( inputStream); return js_void; }
static OBJ readList(OBJ inStream){ int ch; OBJ car, cdr; ch = skipWhiteSpace(inStream); if( ch == ')'){ return js_nil; } unreadChar(inStream, ch); car = js_read(inStream); cdr = readList(inStream); return newCons(car, cdr); }
void jREPL(OBJ input){ /* * Jojo Scheme REPL */ for(;;){ OBJ expr, result; if(prompt_enabled) printf(CYN "JS> " RESET); expr = js_read(input); // R ead result = js_eval(globalEnvironment, expr); // E valuate if( result == js_eof) return; js_print(stdout, result, 1); // P rint // L oop printf("\n"); } }
VOIDPTRFUNC CP_jREPL(){ //fprintf(stdout, "\nStack in CP_jREPL:\n"); //printJStack(__FILE__,__LINE__,__FUNCTION__); DEBUGCODE(PRINT_STACK->state, printJStack(__FILE__,__LINE__,__FUNCTION__)); OBJ inputStream = ARG(0); OBJ expr; VOIDPTRFUNC CP_jREPL2(); if( SP > 5) prompt_off(); if(prompt_enabled) printf(CYN "JS> " RESET); expr = js_read(inputStream); // R ead if( expr == js_eof ) RETURN( js_void ); CALL2( CP_js_eval,globalEnvironment, expr, CP_jREPL2); // E val }
void loop(int sockfd, int jsfd, const struct js_layout &layout) { struct js_event event; Bstrlib::CBString input; jsmath::js_log js(jsfd); std::array<int, 6> motors; while (js_read(jsfd, event) != -1 && !js_quit(event, layout) ) { js.update(event); js.to_motors(layout, motors); jsmath::send_motors(sockfd, motors); /* jsmath::send_motors(0, motors); */ cli_read(sockfd, input); puts(input); } }
OBJ js_read(OBJ inStream){ OBJ retVal; prompt_off(); char ch = skipWhiteSpace(inStream); if( ch == -1){ retVal = js_eof; unreadChar(inStream, ch); } else if(ch == '('){ retVal = readList(inStream); } else if(ch =='\''){ OBJ expr = js_read(inStream); // (quote (expr* (nil))) -> expr must be a cons return newCons(symbolTableGetOrAdd("quote"), newCons(expr, js_nil)); } else if(ch == '"'){ retVal = readString(inStream); } else if(isDigit(ch)){ retVal = readNumber(inStream, ch, 0); }else if(ch == '-'){ /* * TO-DO refactor: implement proper read ahead solution */ // simple read ahead to catch negative numbers char nextCh = nextChar(inStream); if(isDigit(nextCh)){ retVal = readNumber(inStream, nextCh, 1); }else{ unreadChar(inStream, nextCh); retVal = readSymbol(inStream, ch); } } #ifdef DEBUG else if(ch == '%'){ ch = nextChar(inStream); OBJ debugOption; if(ch == '\n' ){ unreadChar(inStream, ch); debugOption = newSymbol(""); }else{ debugOption = readSymbol(inStream, ch); } switchDebugOptions( debugOption ); retVal = js_void; } #endif else { retVal = readSymbol(inStream, ch); } if(thisIsTheEnd(inStream)){ prompt_on(); }; return retVal; }