feat: support function params + stack based compiler!

This commit is contained in:
2025-12-01 13:59:30 +01:00
parent b3498135aa
commit 952df07ce3
5 changed files with 156 additions and 118 deletions

View File

@ -74,7 +74,7 @@ private:
class FnOp : public Op
{
public:
FnOp(StringView name, const CompoundNode* body);
FnOp(StringView name, const CompoundNode* body, const View<StringView>& params);
~FnOp() {}
OP_TYPE(FN)
@ -93,9 +93,11 @@ public:
public:
const StringView& name() const { return m_name; }
const OpView& ops() const { return m_ops; }
const View<StringView>& params() const { return m_params; }
private:
StringView m_name;
OpView m_ops;
View<StringView> m_params;
};
class LoadConstOp : public Op, public Valued
@ -243,11 +245,14 @@ public:
Reg ParseFnCall(const FnCallNode* fn)
{
// TODO: support multiple args
auto arg = ParseExpression(fn->arg());
auto argRegs = RegBuilder();
argRegs.Push(arg);
if (fn->arg() != nullptr)
{
auto arg = ParseExpression(fn->arg());
argRegs.Push(arg);
}
auto dst = AllocateRegister();
m_ops.Push(new CallOp(dst, fn->name(), RegView(argRegs.data, argRegs.size)));
m_ops.Push(new CallOp(dst, fn->name(), argRegs.view()));
return dst;
}
@ -319,7 +324,7 @@ public:
// Functions
for (auto &fn : program->funcs())
{
m_ops.Push(new FnOp(fn->name(), fn->body()));
m_ops.Push(new FnOp(fn->name(), fn->body(), fn->params()));
}
return OpView(m_ops.data, m_ops.size);