ADD: now expose module to macros.

main
bog 2023-09-19 11:29:23 +02:00
parent 073cbb4618
commit 820d26491a
12 changed files with 55 additions and 46 deletions

View File

@ -1,5 +1,6 @@
(assert-fail a) (assert-fail a)
($ a 34) ($ a 34)
(assert-fail (a))
(assert= 34 a) (assert= 34 a)
($ b ($ c 9)) ($ b ($ c 9))

View File

@ -2,6 +2,8 @@
#define COMMON_HPP #define COMMON_HPP
#include "../src/Zarn.hpp" #include "../src/Zarn.hpp"
#include "../src/Module.hpp"
using namespace zn; using namespace zn;
#define STDARGS std::vector<std::shared_ptr<Constant>> #define STDARGS std::vector<std::shared_ptr<Constant>>

View File

@ -1,13 +1,11 @@
#include "macro.hpp" #include "macro.hpp"
void assert_fail(Node const& node, void assert_fail(Node const& node, Module& mod)
Compiler& compiler,
Program& program,
SymTable&)
{ {
try 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(); Loc loc = node.loc();
std::cerr << loc.file_path().string() << ":" << loc.line(); 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)); (TYPE_NIL, node.loc(), 0));
program.append(OPCODE_LOAD_CONST, addr); mod.program().append(OPCODE_LOAD_CONST, addr);
} }
void declare(Node const& node, void declare(Node const& node, Module &mod)
Compiler& compiler,
Program& program,
SymTable& sym)
{ {
std::string ident = node.child_at(1)->repr(); std::string ident = node.child_at(1)->repr();
compiler.compile(*node.child_at(2), program); mod.compiler().compile(*node.child_at(2), mod.program());
auto entry = sym.find_any(ident); auto entry = mod.sym().find_any(ident);
assert(entry); assert(entry);
size_t addr = sym.gen_addr(); size_t addr = mod.sym().gen_addr();
sym.declare(ident, addr); mod.sym().declare(ident, addr);
program.append(OPCODE_STORE_LOCAL, addr); mod.program().append(OPCODE_STORE_LOCAL, addr);
} }

View File

@ -3,14 +3,8 @@
#include "common.hpp" #include "common.hpp"
void assert_fail(Node const& node, void assert_fail(Node const& node, Module& mod);
Compiler& compiler,
Program& program,
SymTable& sym);
void declare(Node const& node, void declare(Node const& node, Module& mod);
Compiler& compiler,
Program& program,
SymTable& sym);
#endif #endif

View File

@ -1,12 +1,16 @@
#include "Compiler.hpp" #include "Compiler.hpp"
#include "Module.hpp"
#include "Node.hpp" #include "Node.hpp"
#include "Program.hpp" #include "Program.hpp"
#include "NativeMacro.hpp" #include "NativeMacro.hpp"
namespace zn namespace zn
{ {
/*explicit*/ Compiler::Compiler(Logger& logger, SymTable& sym) /*explicit*/ Compiler::Compiler(Module& mod,
: m_logger { logger } Logger& logger,
SymTable& sym)
: m_mod { mod }
, m_logger { logger }
, m_sym { sym } , m_sym { sym }
{ {
} }
@ -57,8 +61,7 @@ namespace zn
{ {
if (macro->name() == ident) if (macro->name() == ident)
{ {
macro->execute(node, *this, macro->execute(node, m_mod);
program, m_sym);
found_macro = true; found_macro = true;
break; break;
} }

View File

@ -12,17 +12,19 @@ namespace zn
ZN_ERROR(compile_error); ZN_ERROR(compile_error);
class NativeMacro; class NativeMacro;
class Module;
class Compiler class Compiler
{ {
public: public:
explicit Compiler(Logger& logger, SymTable& sym); explicit Compiler(Module& mod, Logger& logger, SymTable& sym);
virtual ~Compiler(); virtual ~Compiler();
void add_macro(std::shared_ptr<NativeMacro> macro); void add_macro(std::shared_ptr<NativeMacro> macro);
void compile(Node const& node, Program& program); void compile(Node const& node, Program& program);
private: private:
Module& m_mod;
Logger& m_logger; Logger& m_logger;
SymTable& m_sym; SymTable& m_sym;
std::vector<std::shared_ptr<NativeMacro>> m_macros; std::vector<std::shared_ptr<NativeMacro>> m_macros;

View File

@ -1,4 +1,5 @@
#include "Module.hpp" #include "Module.hpp"
#include "Zarn.hpp"
#include "Lexer.hpp" #include "Lexer.hpp"
#include "Parser.hpp" #include "Parser.hpp"
#include "Program.hpp" #include "Program.hpp"
@ -48,9 +49,7 @@ namespace zn
m_zarn.load_std_library(); m_zarn.load_std_library();
StaticPass static_pass { m_logger, m_sym }; m_static_pass.execute(*ast);
static_pass.execute(*ast);
m_compiler.compile(*ast, m_program); m_compiler.compile(*ast, m_program);
m_vm.execute(m_program); m_vm.execute(m_program);

View File

@ -6,6 +6,8 @@
#include "VM.hpp" #include "VM.hpp"
#include "Program.hpp" #include "Program.hpp"
#include "SymTable.hpp" #include "SymTable.hpp"
#include "StaticPass.hpp"
#include "Compiler.hpp"
#include "Zarn.hpp" #include "Zarn.hpp"
namespace zn namespace zn
@ -18,6 +20,12 @@ namespace zn
explicit Module(Logger& logger); explicit Module(Logger& logger);
virtual ~Module(); 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); void load_from_file(std::filesystem::path file_path);
std::string string() const; std::string string() const;
@ -27,7 +35,8 @@ namespace zn
Program m_program; Program m_program;
VM m_vm { m_program }; VM m_vm { m_program };
SymTable m_sym; 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}; Zarn m_zarn {m_compiler, m_sym, m_vm};
}; };
} }

View File

@ -14,10 +14,8 @@ namespace zn
} }
void NativeMacro::execute(Node const& node, void NativeMacro::execute(Node const& node,
Compiler& compiler, Module& mod)
Program& program,
SymTable& sym)
{ {
m_macro(node, compiler, program, sym); m_macro(node, mod);
} }
} }

View File

@ -9,11 +9,11 @@
namespace zn namespace zn
{ {
class Module;
using native_macro_t = std::function<void using native_macro_t = std::function<void
(Node const&, (Node const&,
Compiler&, Module&)>;
Program&,
SymTable&)>;
class NativeMacro class NativeMacro
{ {
@ -25,9 +25,7 @@ namespace zn
void execute(Node const& node, void execute(Node const& node,
Compiler& compiler, Module&);
Program& program,
SymTable& sym);
private: private:
std::string m_name; std::string m_name;

View File

@ -33,7 +33,6 @@ namespace zn
{ {
std::string varname = node.child_at(1)->repr(); std::string varname = node.child_at(1)->repr();
auto vartype = execute(*node.child_at(2)); auto vartype = execute(*node.child_at(2));
//int index = m_sym.gen_addr();
m_sym.prepare(varname, *vartype.type); m_sym.prepare(varname, *vartype.type);
return vartype; return vartype;
@ -45,7 +44,16 @@ namespace zn
} }
auto proto = sym->prototype; 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 if (proto->get_param_count() != node.size() - 1
&& proto->get_param(proto->get_param_count() - 1).tag && proto->get_param(proto->get_param_count() - 1).tag

View File

@ -4,7 +4,7 @@
#include "common.hpp" #include "common.hpp"
#include "SymTable.hpp" #include "SymTable.hpp"
#include "VM.hpp" #include "VM.hpp"
#include "src/NativeMacro.hpp" #include "NativeMacro.hpp"
namespace zn namespace zn
{ {