Commit 3fb931da authored by Tobias WEBER's avatar Tobias WEBER
Browse files

continued with parser for data analysis

parent 1e59b6f2
......@@ -80,6 +80,7 @@ add_executable(in20
${BISON_cliparser_OUTPUT_SOURCE} ${BISON_cliparser_OUTPUT_HEADER}
${FLEX_clilexer_OUTPUTS} ${FLEX_clilexer_OUTPUT_HEADER}
tools/cli/cliparser.cpp tools/cli/cliparser.h tools/cli/cliparser_types.h
tools/cli/ast.cpp
ext/tlibs/log/log.cpp ext/tlibs/file/loadinstr.cpp
......
/**
* Evaluates the command AST
* @author Tobias Weber <tweber@ill.fr>
* @date 15-Jun-2018
* @license see 'LICENSE' file
*/
#include "cliparser.h"
// ----------------------------------------------------------------------------
// evaluation of the AST
// ----------------------------------------------------------------------------
std::shared_ptr<Symbol> CliASTReal::Eval() const
{
return nullptr;
}
std::shared_ptr<Symbol> CliASTString::Eval() const
{
return nullptr;
}
std::shared_ptr<Symbol> CliASTIdent::Eval() const
{
return nullptr;
}
std::shared_ptr<Symbol> CliASTAssign::Eval() const
{
return nullptr;
}
std::shared_ptr<Symbol> CliASTPlus::Eval() const
{
return nullptr;
}
std::shared_ptr<Symbol> CliASTMinus::Eval() const
{
return nullptr;
}
std::shared_ptr<Symbol> CliASTMult::Eval() const
{
return nullptr;
}
std::shared_ptr<Symbol> CliASTDiv::Eval() const
{
return nullptr;
}
std::shared_ptr<Symbol> CliASTMod::Eval() const
{
return nullptr;
}
std::shared_ptr<Symbol> CliASTPow::Eval() const
{
return nullptr;
}
std::shared_ptr<Symbol> CliASTCall::Eval() const
{
return nullptr;
}
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// printing of the AST
// ----------------------------------------------------------------------------
void CliAST::Print(int indent) const
{
if(m_left) m_left->Print(indent+1);
if(m_right) m_right->Print(indent+1);
}
void CliASTReal::Print(int indent) const
{
for(int i=0; i<indent; ++i) std::cout << "\t";
std::cout << "real: " << m_val << "\n";
}
void CliASTString::Print(int indent) const
{
for(int i=0; i<indent; ++i) std::cout << "\t";
std::cout << "string: " << m_val << "\n";
}
void CliASTIdent::Print(int indent) const
{
for(int i=0; i<indent; ++i) std::cout << "\t";
std::cout << "ident: " << m_val << "\n";
}
void CliASTAssign::Print(int indent) const
{
for(int i=0; i<indent; ++i) std::cout << "\t";
std::cout << "op: assign\n";
CliAST::Print(indent);
}
void CliASTPlus::Print(int indent) const
{
for(int i=0; i<indent; ++i) std::cout << "\t";
std::cout << "op: +\n";
CliAST::Print(indent);
}
void CliASTMinus::Print(int indent) const
{
for(int i=0; i<indent; ++i) std::cout << "\t";
std::cout << "op: -\n";
CliAST::Print(indent);
}
void CliASTMult::Print(int indent) const
{
for(int i=0; i<indent; ++i) std::cout << "\t";
std::cout << "op: *\n";
CliAST::Print(indent);
}
void CliASTDiv::Print(int indent) const
{
for(int i=0; i<indent; ++i) std::cout << "\t";
std::cout << "op: /\n";
CliAST::Print(indent);
}
void CliASTMod::Print(int indent) const
{
for(int i=0; i<indent; ++i) std::cout << "\t";
std::cout << "op: %\n";
CliAST::Print(indent);
}
void CliASTPow::Print(int indent) const
{
for(int i=0; i<indent; ++i) std::cout << "\t";
std::cout << "op: ^\n";
CliAST::Print(indent);
}
void CliASTCall::Print(int indent) const
{
for(int i=0; i<indent; ++i) std::cout << "\t";
std::cout << "op: call\n";
CliAST::Print(indent);
}
// ----------------------------------------------------------------------------
......@@ -45,7 +45,7 @@ real (([0-9]+\.?[0-9]*)|(\.[0-9]+))
"^" { return yy::CliParser::make_TOK_POW(); }
"=" { return yy::CliParser::make_TOK_ASSIGN(); }
"\n" { return yy::CliParser::make_TOK_NEWLINE(); }
[;\n] { return yy::CliParser::make_TOK_NEWLINE(); }
[ \t] /* eps */
. {
......
......@@ -57,91 +57,6 @@ extern yy::CliParser::symbol_type yylex(CliParserContext &context)
// ----------------------------------------------------------------------------
// AST
// ----------------------------------------------------------------------------
void CliAST::Print(int indent) const
{
if(m_left) m_left->Print(indent+1);
if(m_right) m_right->Print(indent+1);
}
void CliASTReal::Print(int indent) const
{
for(int i=0; i<indent; ++i) std::cout << "\t";
std::cout << "real: " << m_val << "\n";
}
void CliASTString::Print(int indent) const
{
for(int i=0; i<indent; ++i) std::cout << "\t";
std::cout << "string: " << m_val << "\n";
}
void CliASTIdent::Print(int indent) const
{
for(int i=0; i<indent; ++i) std::cout << "\t";
std::cout << "ident: " << m_val << "\n";
}
void CliASTAssign::Print(int indent) const
{
for(int i=0; i<indent; ++i) std::cout << "\t";
std::cout << "op: assign\n";
CliAST::Print(indent);
}
void CliASTPlus::Print(int indent) const
{
for(int i=0; i<indent; ++i) std::cout << "\t";
std::cout << "op: +\n";
CliAST::Print(indent);
}
void CliASTMinus::Print(int indent) const
{
for(int i=0; i<indent; ++i) std::cout << "\t";
std::cout << "op: -\n";
CliAST::Print(indent);
}
void CliASTMult::Print(int indent) const
{
for(int i=0; i<indent; ++i) std::cout << "\t";
std::cout << "op: *\n";
CliAST::Print(indent);
}
void CliASTDiv::Print(int indent) const
{
for(int i=0; i<indent; ++i) std::cout << "\t";
std::cout << "op: /\n";
CliAST::Print(indent);
}
void CliASTMod::Print(int indent) const
{
for(int i=0; i<indent; ++i) std::cout << "\t";
std::cout << "op: %\n";
CliAST::Print(indent);
}
void CliASTPow::Print(int indent) const
{
for(int i=0; i<indent; ++i) std::cout << "\t";
std::cout << "op: ^\n";
CliAST::Print(indent);
}
void CliASTCall::Print(int indent) const
{
for(int i=0; i<indent; ++i) std::cout << "\t";
std::cout << "op: call\n";
CliAST::Print(indent);
}
// ----------------------------------------------------------------------------
/*
int main()
......
......@@ -18,14 +18,20 @@
#include "cliparser_types.h"
#include "cliparser_impl.h"
#include "tools/in20/data.h"
using t_real = t_real_cli;
class CliAST;
class CliParserContext;
// ----------------------------------------------------------------------------
// Lexer
// ----------------------------------------------------------------------------
class CliParserContext;
class CliLexer : public yyFlexLexer
{
private:
......@@ -45,6 +51,7 @@ template<class t_real> t_real str_to_real(const std::string& str);
// ----------------------------------------------------------------------------
// Parser
// ----------------------------------------------------------------------------
......@@ -53,7 +60,7 @@ class CliParserContext
{
private:
CliLexer m_lex;
std::vector<std::shared_ptr<CliAST>> m_asts;
std::vector<std::string> m_errors;
public:
......@@ -64,12 +71,76 @@ public:
void ClearErrors() { m_errors.clear(); }
void SetLexerInput(std::istream &istr);
void AddAST(std::shared_ptr<CliAST> ast) { m_asts.push_back(ast); }
void ClearASTs() { m_asts.clear(); }
const std::vector<std::shared_ptr<CliAST>>& GetASTs() const { return m_asts; }
};
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// Symbol interface
// ----------------------------------------------------------------------------
class Symbol
{
public:
virtual ~Symbol() {}
virtual std::shared_ptr<Symbol> copy() const = 0;
};
class SymbolReal : public Symbol
{
private:
t_real m_val = 0;
public:
SymbolReal() = default;
SymbolReal(t_real val) : m_val(val) {}
virtual ~SymbolReal() {}
virtual std::shared_ptr<Symbol> copy() const override { return std::make_shared<SymbolReal>(m_val); }
};
class SymbolString : public Symbol
{
private:
std::string m_val;
public:
SymbolString() = default;
SymbolString(const std::string& val) : m_val(val) {}
virtual ~SymbolString() {}
virtual std::shared_ptr<Symbol> copy() const override { return std::make_shared<SymbolString>(m_val); }
};
class SymbolDataset : public Symbol
{
private:
Dataset m_val;
public:
SymbolDataset() = default;
SymbolDataset(const Dataset& val) : m_val(val) {}
virtual ~SymbolDataset() {}
virtual std::shared_ptr<Symbol> copy() const override { return std::make_shared<SymbolDataset>(m_val); }
};
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// AST
// ----------------------------------------------------------------------------
......@@ -87,6 +158,7 @@ public:
void SetRight(std::shared_ptr<CliAST> right) { m_right = right; }
virtual void Print(int indent = 0) const;
virtual std::shared_ptr<Symbol> Eval() const = 0;
};
......@@ -99,6 +171,7 @@ public:
CliASTReal(t_real_cli val) : m_val(val) { }
virtual void Print(int indent = 0) const override;
virtual std::shared_ptr<Symbol> Eval() const override;
};
......@@ -111,6 +184,7 @@ public:
CliASTString(const std::string& val) : m_val(val) { }
virtual void Print(int indent = 0) const override;
virtual std::shared_ptr<Symbol> Eval() const override;
};
......@@ -123,6 +197,7 @@ public:
CliASTIdent(const std::string& val) : m_val(val) { }
virtual void Print(int indent = 0) const override;
virtual std::shared_ptr<Symbol> Eval() const override;
};
......@@ -132,6 +207,7 @@ public:
using CliAST::CliAST;
virtual void Print(int indent = 0) const override;
virtual std::shared_ptr<Symbol> Eval() const override;
};
......@@ -141,6 +217,7 @@ public:
using CliAST::CliAST;
virtual void Print(int indent = 0) const override;
virtual std::shared_ptr<Symbol> Eval() const override;
};
......@@ -150,6 +227,7 @@ public:
using CliAST::CliAST;
virtual void Print(int indent = 0) const override;
virtual std::shared_ptr<Symbol> Eval() const override;
};
......@@ -159,6 +237,7 @@ public:
using CliAST::CliAST;
virtual void Print(int indent = 0) const override;
virtual std::shared_ptr<Symbol> Eval() const override;
};
......@@ -168,6 +247,7 @@ public:
using CliAST::CliAST;
virtual void Print(int indent = 0) const override;
virtual std::shared_ptr<Symbol> Eval() const override;
};
......@@ -177,6 +257,7 @@ public:
using CliAST::CliAST;
virtual void Print(int indent = 0) const override;
virtual std::shared_ptr<Symbol> Eval() const override;
};
......@@ -186,6 +267,7 @@ public:
using CliAST::CliAST;
virtual void Print(int indent = 0) const override;
virtual std::shared_ptr<Symbol> Eval() const override;
};
......@@ -195,6 +277,7 @@ public:
using CliAST::CliAST;
virtual void Print(int indent = 0) const override;
virtual std::shared_ptr<Symbol> Eval() const override;
};
// ----------------------------------------------------------------------------
......
......@@ -36,14 +36,14 @@
commands
: commands command
{ $$ = $2; }
{ context.AddAST($2); }
| /* eps */
{ $$ = nullptr; }
;
command
: expression TOK_NEWLINE
{ $$ = $1; $$->Print(); }
{ $$ = $1; }
| TOK_NEWLINE
{ $$ = nullptr; }
;
......
......@@ -58,9 +58,18 @@ void CommandLineWidget::CommandEntered()
// parse command
std::istringstream istr(cmd.toStdString() + "\n");
m_parsectx.SetLexerInput(istr);
// remove the asts for old commands
m_parsectx.ClearASTs();
yy::CliParser parser(m_parsectx);
int parse_state = parser.parse();
// evaluate commands
for(const auto &ast : m_parsectx.GetASTs())
{
ast->Print(); std::cout.flush();
}
// write error log
for(const auto& err : m_parsectx.GetErrors())
m_pEditHistory->insertHtml((("<b><font color=\"#ff0000\">" + err + "</font></b><br>").c_str()));
......
......@@ -16,7 +16,6 @@
#include <QtWidgets/QTextEdit>
#include <memory>
#include "tools/cli/cliparser.h"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment