ADD: now expose module to macros.
parent
073cbb4618
commit
820d26491a
|
@ -1,5 +1,6 @@
|
|||
(assert-fail a)
|
||||
($ a 34)
|
||||
(assert-fail (a))
|
||||
(assert= 34 a)
|
||||
|
||||
($ b ($ c 9))
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
#define COMMON_HPP
|
||||
|
||||
#include "../src/Zarn.hpp"
|
||||
#include "../src/Module.hpp"
|
||||
|
||||
using namespace zn;
|
||||
|
||||
#define STDARGS std::vector<std::shared_ptr<Constant>>
|
||||
|
|
|
@ -1,13 +1,11 @@
|
|||
#include "macro.hpp"
|
||||
|
||||
void assert_fail(Node const& node,
|
||||
Compiler& compiler,
|
||||
Program& program,
|
||||
SymTable&)
|
||||
void assert_fail(Node const& node, Module& mod)
|
||||
{
|
||||
try
|
||||
{
|
||||
compiler.compile(*node.child_at(1), program);
|
||||
mod.static_pass().execute(*node.child_at(1));
|
||||
mod.compiler().compile(*node.child_at(1), mod.program());
|
||||
Loc loc = node.loc();
|
||||
|
||||
std::cerr << loc.file_path().string() << ":" << loc.line();
|
||||
|
@ -18,22 +16,19 @@ void assert_fail(Node const& node,
|
|||
{
|
||||
}
|
||||
|
||||
size_t addr = program.add_constant(std::make_shared<Constant>
|
||||
size_t addr = mod.program().add_constant(std::make_shared<Constant>
|
||||
(TYPE_NIL, node.loc(), 0));
|
||||
program.append(OPCODE_LOAD_CONST, addr);
|
||||
mod.program().append(OPCODE_LOAD_CONST, addr);
|
||||
}
|
||||
|
||||
void declare(Node const& node,
|
||||
Compiler& compiler,
|
||||
Program& program,
|
||||
SymTable& sym)
|
||||
void declare(Node const& node, Module &mod)
|
||||
{
|
||||
std::string ident = node.child_at(1)->repr();
|
||||
compiler.compile(*node.child_at(2), program);
|
||||
auto entry = sym.find_any(ident);
|
||||
mod.compiler().compile(*node.child_at(2), mod.program());
|
||||
auto entry = mod.sym().find_any(ident);
|
||||
assert(entry);
|
||||
size_t addr = sym.gen_addr();
|
||||
size_t addr = mod.sym().gen_addr();
|
||||
|
||||
sym.declare(ident, addr);
|
||||
program.append(OPCODE_STORE_LOCAL, addr);
|
||||
mod.sym().declare(ident, addr);
|
||||
mod.program().append(OPCODE_STORE_LOCAL, addr);
|
||||
}
|
||||
|
|
|
@ -3,14 +3,8 @@
|
|||
|
||||
#include "common.hpp"
|
||||
|
||||
void assert_fail(Node const& node,
|
||||
Compiler& compiler,
|
||||
Program& program,
|
||||
SymTable& sym);
|
||||
void assert_fail(Node const& node, Module& mod);
|
||||
|
||||
void declare(Node const& node,
|
||||
Compiler& compiler,
|
||||
Program& program,
|
||||
SymTable& sym);
|
||||
void declare(Node const& node, Module& mod);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,12 +1,16 @@
|
|||
#include "Compiler.hpp"
|
||||
#include "Module.hpp"
|
||||
#include "Node.hpp"
|
||||
#include "Program.hpp"
|
||||
#include "NativeMacro.hpp"
|
||||
|
||||
namespace zn
|
||||
{
|
||||
/*explicit*/ Compiler::Compiler(Logger& logger, SymTable& sym)
|
||||
: m_logger { logger }
|
||||
/*explicit*/ Compiler::Compiler(Module& mod,
|
||||
Logger& logger,
|
||||
SymTable& sym)
|
||||
: m_mod { mod }
|
||||
, m_logger { logger }
|
||||
, m_sym { sym }
|
||||
{
|
||||
}
|
||||
|
@ -57,8 +61,7 @@ namespace zn
|
|||
{
|
||||
if (macro->name() == ident)
|
||||
{
|
||||
macro->execute(node, *this,
|
||||
program, m_sym);
|
||||
macro->execute(node, m_mod);
|
||||
found_macro = true;
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -12,17 +12,19 @@ namespace zn
|
|||
ZN_ERROR(compile_error);
|
||||
|
||||
class NativeMacro;
|
||||
class Module;
|
||||
|
||||
class Compiler
|
||||
{
|
||||
public:
|
||||
explicit Compiler(Logger& logger, SymTable& sym);
|
||||
explicit Compiler(Module& mod, Logger& logger, SymTable& sym);
|
||||
virtual ~Compiler();
|
||||
|
||||
void add_macro(std::shared_ptr<NativeMacro> macro);
|
||||
void compile(Node const& node, Program& program);
|
||||
|
||||
private:
|
||||
Module& m_mod;
|
||||
Logger& m_logger;
|
||||
SymTable& m_sym;
|
||||
std::vector<std::shared_ptr<NativeMacro>> m_macros;
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include "Module.hpp"
|
||||
#include "Zarn.hpp"
|
||||
#include "Lexer.hpp"
|
||||
#include "Parser.hpp"
|
||||
#include "Program.hpp"
|
||||
|
@ -48,9 +49,7 @@ namespace zn
|
|||
|
||||
m_zarn.load_std_library();
|
||||
|
||||
StaticPass static_pass { m_logger, m_sym };
|
||||
static_pass.execute(*ast);
|
||||
|
||||
m_static_pass.execute(*ast);
|
||||
m_compiler.compile(*ast, m_program);
|
||||
|
||||
m_vm.execute(m_program);
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
#include "VM.hpp"
|
||||
#include "Program.hpp"
|
||||
#include "SymTable.hpp"
|
||||
#include "StaticPass.hpp"
|
||||
#include "Compiler.hpp"
|
||||
#include "Zarn.hpp"
|
||||
|
||||
namespace zn
|
||||
|
@ -18,6 +20,12 @@ namespace zn
|
|||
explicit Module(Logger& logger);
|
||||
virtual ~Module();
|
||||
|
||||
Compiler& compiler() { return m_compiler; }
|
||||
StaticPass& static_pass() { return m_static_pass; }
|
||||
|
||||
Program& program() { return m_program; }
|
||||
SymTable& sym() { return m_sym; }
|
||||
|
||||
void load_from_file(std::filesystem::path file_path);
|
||||
|
||||
std::string string() const;
|
||||
|
@ -27,7 +35,8 @@ namespace zn
|
|||
Program m_program;
|
||||
VM m_vm { m_program };
|
||||
SymTable m_sym;
|
||||
Compiler m_compiler {m_logger, m_sym};
|
||||
StaticPass m_static_pass {m_logger, m_sym};
|
||||
Compiler m_compiler {*this, m_logger, m_sym};
|
||||
Zarn m_zarn {m_compiler, m_sym, m_vm};
|
||||
};
|
||||
}
|
||||
|
|
|
@ -14,10 +14,8 @@ namespace zn
|
|||
}
|
||||
|
||||
void NativeMacro::execute(Node const& node,
|
||||
Compiler& compiler,
|
||||
Program& program,
|
||||
SymTable& sym)
|
||||
Module& mod)
|
||||
{
|
||||
m_macro(node, compiler, program, sym);
|
||||
m_macro(node, mod);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,11 +9,11 @@
|
|||
|
||||
namespace zn
|
||||
{
|
||||
class Module;
|
||||
|
||||
using native_macro_t = std::function<void
|
||||
(Node const&,
|
||||
Compiler&,
|
||||
Program&,
|
||||
SymTable&)>;
|
||||
Module&)>;
|
||||
|
||||
class NativeMacro
|
||||
{
|
||||
|
@ -25,9 +25,7 @@ namespace zn
|
|||
|
||||
|
||||
void execute(Node const& node,
|
||||
Compiler& compiler,
|
||||
Program& program,
|
||||
SymTable& sym);
|
||||
Module&);
|
||||
|
||||
private:
|
||||
std::string m_name;
|
||||
|
|
|
@ -33,7 +33,6 @@ namespace zn
|
|||
{
|
||||
std::string varname = node.child_at(1)->repr();
|
||||
auto vartype = execute(*node.child_at(2));
|
||||
//int index = m_sym.gen_addr();
|
||||
m_sym.prepare(varname, *vartype.type);
|
||||
|
||||
return vartype;
|
||||
|
@ -45,7 +44,16 @@ namespace zn
|
|||
}
|
||||
|
||||
auto proto = sym->prototype;
|
||||
assert(proto);
|
||||
|
||||
if (!proto)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "cannot call a none function variable '"
|
||||
<< ident
|
||||
<< "'";
|
||||
|
||||
m_logger.log<static_error>(LOG_ERROR, node.loc(), ss.str());
|
||||
}
|
||||
|
||||
if (proto->get_param_count() != node.size() - 1
|
||||
&& proto->get_param(proto->get_param_count() - 1).tag
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
#include "common.hpp"
|
||||
#include "SymTable.hpp"
|
||||
#include "VM.hpp"
|
||||
#include "src/NativeMacro.hpp"
|
||||
#include "NativeMacro.hpp"
|
||||
|
||||
namespace zn
|
||||
{
|
||||
|
|
Reference in New Issue