mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-24 13:30:18 +00:00
fix a bug of REPL
This commit is contained in:
parent
a7f3cab132
commit
84cdb20687
@ -67,6 +67,7 @@ struct CodeObject {
|
|||||||
int prev_line = -1;
|
int prev_line = -1;
|
||||||
for(int i=0; i<co_code.size(); i++){
|
for(int i=0; i<co_code.size(); i++){
|
||||||
const ByteCode& byte = co_code[i];
|
const ByteCode& byte = co_code[i];
|
||||||
|
if(byte.op == OP_NO_OP) continue;
|
||||||
_Str line = std::to_string(byte.line);
|
_Str line = std::to_string(byte.line);
|
||||||
if(byte.line == prev_line) line = "";
|
if(byte.line == prev_line) line = "";
|
||||||
else{
|
else{
|
||||||
|
|||||||
@ -44,6 +44,8 @@ public:
|
|||||||
std::stack<_Code> codes;
|
std::stack<_Code> codes;
|
||||||
std::stack<Loop> loops;
|
std::stack<Loop> loops;
|
||||||
|
|
||||||
|
CompileMode mode;
|
||||||
|
|
||||||
bool isCompilingClass = false;
|
bool isCompilingClass = false;
|
||||||
|
|
||||||
_Str path = "<?>";
|
_Str path = "<?>";
|
||||||
@ -59,13 +61,10 @@ public:
|
|||||||
return loops.top();
|
return loops.top();
|
||||||
}
|
}
|
||||||
|
|
||||||
CompileMode mode() {
|
|
||||||
return getCode()->mode;
|
|
||||||
}
|
|
||||||
|
|
||||||
Compiler(VM* vm, const char* source, _Code code){
|
Compiler(VM* vm, const char* source, _Code code){
|
||||||
this->vm = vm;
|
this->vm = vm;
|
||||||
this->codes.push(code);
|
this->codes.push(code);
|
||||||
|
this->mode = code->mode;
|
||||||
if (!code->co_filename.empty()) path = code->co_filename;
|
if (!code->co_filename.empty()) path = code->co_filename;
|
||||||
this->parser = std::make_unique<Parser>(source);
|
this->parser = std::make_unique<Parser>(source);
|
||||||
|
|
||||||
@ -281,7 +280,7 @@ public:
|
|||||||
consumed = true;
|
consumed = true;
|
||||||
}
|
}
|
||||||
if (repl_throw && peek() == TK("@eof")){
|
if (repl_throw && peek() == TK("@eof")){
|
||||||
throw NeedMoreLines();
|
throw NeedMoreLines(isCompilingClass);
|
||||||
}
|
}
|
||||||
return consumed;
|
return consumed;
|
||||||
}
|
}
|
||||||
@ -579,7 +578,7 @@ __LISTCOMP:
|
|||||||
|
|
||||||
void __compileBlockBody(CompilerAction action) {
|
void __compileBlockBody(CompilerAction action) {
|
||||||
consume(TK(":"));
|
consume(TK(":"));
|
||||||
if(!matchNewLines(mode()==SINGLE_MODE)){
|
if(!matchNewLines(mode==SINGLE_MODE)){
|
||||||
throw SyntaxError(path, parser->previous, "expected a new line after ':'");
|
throw SyntaxError(path, parser->previous, "expected a new line after ':'");
|
||||||
}
|
}
|
||||||
consume(TK("@indent"));
|
consume(TK("@indent"));
|
||||||
@ -745,7 +744,7 @@ __LISTCOMP:
|
|||||||
// If last op is not an assignment, pop the result.
|
// If last op is not an assignment, pop the result.
|
||||||
uint8_t lastOp = getCode()->co_code.back().op;
|
uint8_t lastOp = getCode()->co_code.back().op;
|
||||||
if( lastOp != OP_STORE_NAME_PTR && lastOp != OP_STORE_PTR){
|
if( lastOp != OP_STORE_NAME_PTR && lastOp != OP_STORE_PTR){
|
||||||
if(mode()==SINGLE_MODE && parser->indents.top() == 0){
|
if(mode==SINGLE_MODE && parser->indents.top() == 0){
|
||||||
emitCode(OP_PRINT_EXPR);
|
emitCode(OP_PRINT_EXPR);
|
||||||
}
|
}
|
||||||
emitCode(OP_POP_TOP);
|
emitCode(OP_POP_TOP);
|
||||||
@ -853,7 +852,7 @@ __LITERAL_EXIT:
|
|||||||
lexToken();
|
lexToken();
|
||||||
matchNewLines();
|
matchNewLines();
|
||||||
|
|
||||||
if(mode() == EVAL_MODE) {
|
if(mode == EVAL_MODE) {
|
||||||
EXPR_TUPLE();
|
EXPR_TUPLE();
|
||||||
consume(TK("@eof"));
|
consume(TK("@eof"));
|
||||||
return;
|
return;
|
||||||
|
|||||||
@ -7,7 +7,11 @@
|
|||||||
|
|
||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
|
|
||||||
class NeedMoreLines : public std::exception {};
|
class NeedMoreLines : public std::exception {
|
||||||
|
public:
|
||||||
|
NeedMoreLines(bool isClassDef) : isClassDef(isClassDef) {}
|
||||||
|
bool isClassDef;
|
||||||
|
};
|
||||||
|
|
||||||
class SyntaxError : public std::exception {
|
class SyntaxError : public std::exception {
|
||||||
private:
|
private:
|
||||||
|
|||||||
14
src/main.cpp
14
src/main.cpp
@ -40,7 +40,8 @@ VM* newVM(){
|
|||||||
void REPL(){
|
void REPL(){
|
||||||
std::cout << "pocketpy 0.1.0" << std::endl;
|
std::cout << "pocketpy 0.1.0" << std::endl;
|
||||||
|
|
||||||
bool need_more_lines = false;
|
int need_more_lines = 0;
|
||||||
|
|
||||||
std::string buffer;
|
std::string buffer;
|
||||||
VM* vm = newVM();
|
VM* vm = newVM();
|
||||||
|
|
||||||
@ -54,12 +55,16 @@ void REPL(){
|
|||||||
buffer += line;
|
buffer += line;
|
||||||
buffer += '\n';
|
buffer += '\n';
|
||||||
int n = buffer.size();
|
int n = buffer.size();
|
||||||
if(n>=2 && buffer[n-1]=='\n' && buffer[n-2]=='\n'){
|
if(n>=need_more_lines){
|
||||||
need_more_lines = false;
|
for(int i=buffer.size()-need_more_lines; i<buffer.size(); i++){
|
||||||
|
if(buffer[i] != '\n') goto __NOT_ENOUGH_LINES;
|
||||||
|
}
|
||||||
|
need_more_lines = 0;
|
||||||
line = buffer;
|
line = buffer;
|
||||||
mode = EXEC_MODE; // tmp set to EXEC_MODE
|
mode = EXEC_MODE; // tmp set to EXEC_MODE
|
||||||
buffer.clear();
|
buffer.clear();
|
||||||
}else{
|
}else{
|
||||||
|
__NOT_ENOUGH_LINES:
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
@ -74,9 +79,10 @@ void REPL(){
|
|||||||
#else
|
#else
|
||||||
}catch(std::exception& e){
|
}catch(std::exception& e){
|
||||||
#endif
|
#endif
|
||||||
if(need_more_lines = dynamic_cast<NeedMoreLines*>(&e)){
|
if(dynamic_cast<NeedMoreLines*>(&e)){
|
||||||
buffer += line;
|
buffer += line;
|
||||||
buffer += '\n';
|
buffer += '\n';
|
||||||
|
need_more_lines = e.isClassDef ? 3 : 2;
|
||||||
}else{
|
}else{
|
||||||
vm->printFn(e.what());
|
vm->printFn(e.what());
|
||||||
vm->printFn("\n");
|
vm->printFn("\n");
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user