This commit is contained in:
blueloveTH 2022-11-20 18:41:57 +08:00
parent 027473012b
commit faad1d7067
5 changed files with 28 additions and 6 deletions

View File

@ -81,6 +81,7 @@ public:
rules[TK("False")] = { METHOD(exprValue), NO_INFIX }; rules[TK("False")] = { METHOD(exprValue), NO_INFIX };
rules[TK("lambda")] = { METHOD(exprLambda), NO_INFIX }; rules[TK("lambda")] = { METHOD(exprLambda), NO_INFIX };
rules[TK("None")] = { METHOD(exprValue), NO_INFIX }; rules[TK("None")] = { METHOD(exprValue), NO_INFIX };
rules[TK("...")] = { METHOD(exprValue), NO_INFIX };
rules[TK("@id")] = { METHOD(exprName), NO_INFIX }; rules[TK("@id")] = { METHOD(exprName), NO_INFIX };
rules[TK("@num")] = { METHOD(exprLiteral), NO_INFIX }; rules[TK("@num")] = { METHOD(exprLiteral), NO_INFIX };
rules[TK("@str")] = { METHOD(exprLiteral), NO_INFIX }; rules[TK("@str")] = { METHOD(exprLiteral), NO_INFIX };
@ -190,10 +191,21 @@ public:
case '[': parser->setNextToken(TK("[")); return; case '[': parser->setNextToken(TK("[")); return;
case ']': parser->setNextToken(TK("]")); return; case ']': parser->setNextToken(TK("]")); return;
case '%': parser->setNextToken(TK("%")); return; case '%': parser->setNextToken(TK("%")); return;
case '.': parser->setNextToken(TK(".")); return;
case '&': parser->setNextToken(TK("&")); return; case '&': parser->setNextToken(TK("&")); return;
case '|': parser->setNextToken(TK("|")); return; case '|': parser->setNextToken(TK("|")); return;
case '^': parser->setNextToken(TK("^")); return; case '^': parser->setNextToken(TK("^")); return;
case '.': {
if(parser->matchChar('.')) {
if(parser->matchChar('.')) {
parser->setNextToken(TK("..."));
} else {
syntaxError("invalid token '..'");
}
} else {
parser->setNextToken(TK("."));
}
return;
}
case '=': parser->setNextTwoCharToken('=', TK("="), TK("==")); return; case '=': parser->setNextTwoCharToken('=', TK("="), TK("==")); return;
case '+': parser->setNextTwoCharToken('=', TK("+"), TK("+=")); return; case '+': parser->setNextTwoCharToken('=', TK("+"), TK("+=")); return;
case '>': { case '>': {
@ -575,9 +587,10 @@ __LISTCOMP:
void exprValue() { void exprValue() {
_TokenType op = parser->previous.type; _TokenType op = parser->previous.type;
switch (op) { switch (op) {
case TK("None"): emitCode(OP_LOAD_NONE); break; case TK("None"): emitCode(OP_LOAD_NONE); break;
case TK("True"): emitCode(OP_LOAD_TRUE); break; case TK("True"): emitCode(OP_LOAD_TRUE); break;
case TK("False"): emitCode(OP_LOAD_FALSE); break; case TK("False"): emitCode(OP_LOAD_FALSE); break;
case TK("..."): emitCode(OP_LOAD_ELLIPSIS); break;
default: UNREACHABLE(); default: UNREACHABLE();
} }
} }
@ -876,6 +889,7 @@ __LISTCOMP:
if(match(TK("True"))) return vm->PyBool(true); if(match(TK("True"))) return vm->PyBool(true);
if(match(TK("False"))) return vm->PyBool(false); if(match(TK("False"))) return vm->PyBool(false);
if(match(TK("None"))) return vm->None; if(match(TK("None"))) return vm->None;
if(match(TK("..."))) return vm->Ellipsis;
syntaxError(_Str("expect a literal, not ") + TK_STR(parser->current.type)); syntaxError(_Str("expect a literal, not ") + TK_STR(parser->current.type));
return nullptr; return nullptr;
} }

View File

@ -40,6 +40,7 @@ OPCODE(LOAD_TRUE)
OPCODE(LOAD_FALSE) OPCODE(LOAD_FALSE)
OPCODE(LOAD_EVAL_FN) // load eval() callable into stack OPCODE(LOAD_EVAL_FN) // load eval() callable into stack
OPCODE(LOAD_LAMBDA) // LOAD_CONST + set __module__ attr OPCODE(LOAD_LAMBDA) // LOAD_CONST + set __module__ attr
OPCODE(LOAD_ELLIPSIS)
OPCODE(ASSERT) OPCODE(ASSERT)
OPCODE(RAISE_ERROR) OPCODE(RAISE_ERROR)

View File

@ -7,7 +7,7 @@ typedef uint8_t _TokenType;
constexpr const char* __TOKENS[] = { constexpr const char* __TOKENS[] = {
"@error", "@eof", "@eol", "@sof", "@error", "@eof", "@eol", "@sof",
".", ",", ":", ";", "#", "(", ")", "[", "]", "{", "}", "%", ".", ",", ":", ";", "#", "(", ")", "[", "]", "{", "}", "%",
"+", "-", "*", "/", "//", "**", "=", ">", "<", "+", "-", "*", "/", "//", "**", "=", ">", "<", "...",
"<<", ">>", "&", "|", "^", "<<", ">>", "&", "|", "^",
"==", "!=", ">=", "<=", "==", "!=", ">=", "<=",
"+=", "-=", "*=", "/=", "//=", "+=", "-=", "*=", "/=", "//=",

View File

@ -544,6 +544,10 @@ void __initializeBuiltinFunctions(VM* _vm) {
bool _obj = vm->PyBool_AS_C(args[1]); bool _obj = vm->PyBool_AS_C(args[1]);
return vm->PyBool(_self ^ _obj); return vm->PyBool(_self ^ _obj);
}); });
_vm->bindMethod("ellipsis", "__repr__", [](VM* vm, const pkpy::ArgList& args) {
return vm->PyStr("Ellipsis");
});
} }
#include "builtins.h" #include "builtins.h"

View File

@ -212,6 +212,7 @@ private:
case OP_LOAD_NONE: frame->push(None); break; case OP_LOAD_NONE: frame->push(None); break;
case OP_LOAD_TRUE: frame->push(True); break; case OP_LOAD_TRUE: frame->push(True); break;
case OP_LOAD_FALSE: frame->push(False); break; case OP_LOAD_FALSE: frame->push(False); break;
case OP_LOAD_ELLIPSIS: frame->push(Ellipsis); break;
case OP_ASSERT: case OP_ASSERT:
{ {
PyVar expr = frame->popValue(this); PyVar expr = frame->popValue(this);
@ -326,7 +327,7 @@ private:
public: public:
PyVarDict _types; PyVarDict _types;
PyVar None, True, False; PyVar None, True, False, Ellipsis;
bool use_stdio; bool use_stdio;
std::ostream* _stdout; std::ostream* _stdout;
@ -698,6 +699,7 @@ public:
_tp_pointer = newClassType("_pointer"); _tp_pointer = newClassType("_pointer");
newClassType("NoneType"); newClassType("NoneType");
newClassType("ellipsis");
_tp_function = newClassType("function"); _tp_function = newClassType("function");
_tp_native_function = newClassType("_native_function"); _tp_native_function = newClassType("_native_function");
@ -705,6 +707,7 @@ public:
_tp_bounded_method = newClassType("_bounded_method"); _tp_bounded_method = newClassType("_bounded_method");
this->None = newObject(_types["NoneType"], (_Int)0); this->None = newObject(_types["NoneType"], (_Int)0);
this->Ellipsis = newObject(_types["ellipsis"], (_Int)0);
this->True = newObject(_tp_bool, true); this->True = newObject(_tp_bool, true);
this->False = newObject(_tp_bool, false); this->False = newObject(_tp_bool, false);
this->builtins = newModule("builtins"); this->builtins = newModule("builtins");