feat: implement functions
This commit is contained in:
@@ -18,6 +18,9 @@ typedef enum
|
||||
FZL_OPCODE_JMP = 0xF20001, // Jump to specified IP address
|
||||
FZL_OPCODE_JZ = 0xF20002, // Jump if top value in dstack is zero
|
||||
|
||||
FZL_OPCODE_CALL = 0xF30001, // Call function
|
||||
FZL_OPCODE_RET = 0xF30002, // Return from function
|
||||
|
||||
FZL_OPCODE_ADD = 0xA00001, // Sum two numbers in dstack
|
||||
FZL_OPCODE_SUB = 0xA00002, // Subract two numbers in dstack
|
||||
FZL_OPCODE_MUL = 0xA00003, // Multiply two numbers in dstack
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef FZL_PARSER_H
|
||||
#define FZL_PARSER_H
|
||||
|
||||
#include "common/fzl_types.h"
|
||||
#include <compiler/fzl_reader.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <stddef.h>
|
||||
@@ -13,6 +14,8 @@ typedef enum
|
||||
FZL_NODETYPE_LITERAL,
|
||||
FZL_NODETYPE_IDENTIFIER,
|
||||
FZL_NODETYPE_CONDITIONAL,
|
||||
FZL_NODETYPE_FUNCTION,
|
||||
FZL_NODETYPE_RETURN,
|
||||
|
||||
FZL_NODETYPE_ADD,
|
||||
FZL_NODETYPE_SUB,
|
||||
@@ -93,6 +96,23 @@ typedef struct fzl_node
|
||||
struct fzl_node *true_block;
|
||||
struct fzl_node *else_block;
|
||||
} cond;
|
||||
|
||||
// Function declaration
|
||||
struct
|
||||
{
|
||||
struct fzl_node **args;
|
||||
size_t args_count;
|
||||
|
||||
struct fzl_node *body;
|
||||
fzl_token_t *name;
|
||||
fzl_token_t *type;
|
||||
} fn;
|
||||
|
||||
// Return value
|
||||
struct
|
||||
{
|
||||
struct fzl_node *value;
|
||||
} ret;
|
||||
};
|
||||
} fzl_node_t;
|
||||
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
// Token type
|
||||
typedef enum
|
||||
{
|
||||
FZL_TOKTYPE_RETURN, // return
|
||||
|
||||
FZL_TOKTYPE_IF, // if
|
||||
FZL_TOKTYPE_ELSE, // else
|
||||
|
||||
|
||||
@@ -18,6 +18,13 @@ typedef struct
|
||||
bool error;
|
||||
} fzl_walk_res_t;
|
||||
|
||||
// Function context descriptor
|
||||
typedef struct
|
||||
{
|
||||
fzl_node_t *fn;
|
||||
size_t initial_env_lvl;
|
||||
} fzl_walk_ctx_t;
|
||||
|
||||
// Walker descriptor
|
||||
typedef struct
|
||||
{
|
||||
@@ -30,6 +37,8 @@ typedef struct
|
||||
|
||||
fzl_symtable_t *syms[64];
|
||||
size_t sym_top;
|
||||
|
||||
fzl_walk_ctx_t *ctx;
|
||||
} fzl_walker_t;
|
||||
|
||||
// Allocate new walker
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef enum
|
||||
{
|
||||
@@ -17,6 +18,7 @@ typedef struct
|
||||
{
|
||||
char *name;
|
||||
fzl_type_t type;
|
||||
size_t ip;
|
||||
} fzl_sym_t;
|
||||
|
||||
// Symbols table descriptor
|
||||
@@ -33,7 +35,7 @@ fzl_symtable_t *fzl_mksymt();
|
||||
|
||||
// Put new entry into symtable
|
||||
fzl_symtable_res_t fzl_symt_put(fzl_symtable_t *symt, char *name,
|
||||
fzl_type_t type);
|
||||
fzl_type_t type, uint64_t ip);
|
||||
|
||||
// Get entry from specified symtable
|
||||
bool fzl_symt_get(fzl_symtable_t *symt, char *name, fzl_sym_t *res);
|
||||
|
||||
@@ -33,7 +33,7 @@ typedef struct
|
||||
fzl_vm_env_t *fzl_mkenv();
|
||||
|
||||
// Try get value by name
|
||||
fzl_value_t fzl_env_lookup(fzl_vm_env_t *env, char *name);
|
||||
bool fzl_env_lookup(fzl_vm_env_t *env, char *name, fzl_value_t *res);
|
||||
|
||||
// Check name in env table
|
||||
bool fzl_env_contains(fzl_vm_env_t *env, char *name);
|
||||
|
||||
Reference in New Issue
Block a user