Prompts in calc
This commit is contained in:
parent
a93d4427e2
commit
5c2b4b7fa0
|
@ -11,14 +11,16 @@ grammar:
|
|||
| [toplevel]
|
||||
;
|
||||
|
||||
<toplevel> ::= <toplevel_expr> "SEMICOLON" [toplevel]
|
||||
| <functiondef> "SEMICOLON" [toplevel]
|
||||
| <assign> "SEMICOLON" [toplevel]
|
||||
| <toplevel_error> "SEMICOLON" [toplevel]
|
||||
<toplevel> ::= <toplevel_expr> "SEMICOLON" [toplevel]
|
||||
| <toplevel_functiondef> "SEMICOLON" [toplevel]
|
||||
| <toplevel_assign> "SEMICOLON" [toplevel]
|
||||
| <toplevel_error> "SEMICOLON" [toplevel]
|
||||
;
|
||||
|
||||
<toplevel_expr> ::= <expr> [toplevel];
|
||||
<toplevel_error> ::= <error> [toplevel];
|
||||
<toplevel_expr> ::= <expr> [toplevel];
|
||||
<toplevel_functiondef> ::= <functiondef> [toplevel];
|
||||
<toplevel_assign> ::= <assign> [toplevel];
|
||||
<toplevel_error> ::= <error> [toplevel];
|
||||
|
||||
<expr> ::= <expr> "PLUS" <expr> [binop]
|
||||
| <expr> "MINUS" <expr> [binop]
|
||||
|
|
|
@ -4,4 +4,5 @@
|
|||
int main() {
|
||||
calc::Parser p(CalcLexer{std::cin});
|
||||
p.parse();
|
||||
std::cout << "\033[1;33mGoodbye\033[0m" << std::endl;
|
||||
}
|
||||
|
|
|
@ -4,20 +4,24 @@
|
|||
|
||||
namespace calc {
|
||||
|
||||
Parser::Parser(CalcLexer lex) : m_lex(lex)
|
||||
{}
|
||||
Parser::Parser(CalcLexer lex) : m_lex(lex) {
|
||||
std::cout << ">>> ";
|
||||
std::cout.flush();
|
||||
}
|
||||
|
||||
Parser::Token Parser::lex() {
|
||||
while (true) {
|
||||
try {
|
||||
CalcLexer::Token orig = m_lex.nextToken();
|
||||
Token ret{calcParser_Symbol::T_EOF, nullptr};
|
||||
if (orig.type == CalcLexer::NUM) {
|
||||
return { calcParser_Symbol::T_NUM, std::make_unique<Number>(std::stod(orig.content)) };
|
||||
ret = { calcParser_Symbol::T_NUM, std::make_unique<Number>(std::stod(orig.content)) };
|
||||
} else if (orig.type == CalcLexer::IDENT) {
|
||||
return { calcParser_Symbol::T_IDENT, std::make_unique<Var>(orig.content) };
|
||||
ret = { calcParser_Symbol::T_IDENT, std::make_unique<Var>(orig.content) };
|
||||
} else {
|
||||
return { static_cast<calcParser_Symbol>(orig.type), nullptr };
|
||||
ret = { static_cast<calcParser_Symbol>(orig.type), nullptr };
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
catch (CalcLexer::NoMoreTokens) {
|
||||
return { calcParser_Symbol::T_EOF, nullptr };
|
||||
|
@ -36,10 +40,17 @@ Parser::Value Parser::error(Token, const std::vector<calcParser_Symbol>&) {
|
|||
Parser::Value Parser::reduce_toplevel(std::deque<Token> subparts) {
|
||||
if (subparts.empty()) return nullptr;
|
||||
if (subparts[0].symbol == calcParser_Symbol::V_expr) {
|
||||
std::cout << "=> " << subparts[0].value->eval(m_variables, m_functions) << std::endl;
|
||||
std::cout << "\033[3;34m => " << subparts[0].value->eval(m_variables, m_functions) << "\033[0m" << std::endl;
|
||||
} else if (subparts[0].symbol == calcParser_Symbol::V_error) {
|
||||
std::cout << "=> <syntax error>" << std::endl;
|
||||
std::cout << "\033[3;31m => <syntax error>" << "\033[0m" << std::endl;
|
||||
}
|
||||
try {
|
||||
if (m_lex.peek() == '\n') {
|
||||
std::cout << ">>> ";
|
||||
std::cout.flush();
|
||||
}
|
||||
}
|
||||
catch (CalcLexer::NoMoreTokens) {}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
@ -114,7 +125,7 @@ Parser::Value Parser::reduce_functiondef(std::deque<Token> subparts) {
|
|||
}
|
||||
|
||||
m_functions[name] = std::make_unique<Function>(std::move(formalParams), std::move(subparts[6].value));
|
||||
std::cout << "=> Function " << name << " defined." << std::endl;
|
||||
std::cout << "\033[3;32m => Function " << name << " defined.\033[0m" << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
@ -141,7 +152,7 @@ Parser::Value Parser::reduce_idents(std::deque<Token> subparts) {
|
|||
Parser::Value Parser::reduce_assign(std::deque<Token> subparts) {
|
||||
std::string name = dynamic_cast<Var*>(subparts[0].value.get())->getName();
|
||||
m_variables[name] = subparts[2].value->eval(m_variables, m_functions);
|
||||
std::cout << "=> " << name << " = " << m_variables[name] << std::endl;
|
||||
std::cout << "\033[3;34m => " << name << " = " << m_variables[name] << "\033[0m" <<std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue