feat: implement objects and classes
This commit is contained in:
@@ -21,6 +21,10 @@ typedef enum
|
||||
FZL_OPCODE_CALL = 0xF30001, // Call function
|
||||
FZL_OPCODE_RET = 0xF30002, // Return from function
|
||||
|
||||
FZL_OPCODE_ALLOC_OBJECT = 0xFA0001, // Allocate N slots
|
||||
FZL_OPCODE_GET_FIELD = 0xFA0002, // Get field N of an object at dstack
|
||||
FZL_OPCODE_SET_FIELD = 0xFA0003, // Set field N of an object at dstack
|
||||
|
||||
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
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
typedef enum
|
||||
{
|
||||
FZL_TYPE_NULL,
|
||||
FZL_TYPE_OBJECT,
|
||||
|
||||
FZL_TYPE_STRING,
|
||||
FZL_TYPE_CHAR,
|
||||
|
||||
@@ -13,6 +13,8 @@ typedef struct
|
||||
|
||||
union
|
||||
{
|
||||
void *obj;
|
||||
|
||||
struct
|
||||
{
|
||||
const char *value;
|
||||
|
||||
@@ -17,6 +17,7 @@ typedef enum
|
||||
FZL_NODETYPE_RETURN,
|
||||
FZL_NODETYPE_CLASS,
|
||||
FZL_NODETYPE_FIELD,
|
||||
FZL_NODETYPE_CONSTRUCT,
|
||||
|
||||
FZL_NODETYPE_ADD,
|
||||
FZL_NODETYPE_SUB,
|
||||
@@ -133,6 +134,12 @@ typedef struct fzl_node
|
||||
struct fzl_node *parent;
|
||||
struct fzl_node *field;
|
||||
} memb;
|
||||
|
||||
// Object construct
|
||||
struct
|
||||
{
|
||||
struct fzl_node *name;
|
||||
} construct;
|
||||
};
|
||||
} fzl_node_t;
|
||||
|
||||
|
||||
@@ -5,10 +5,20 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/utils/fzl_const_pool.h>
|
||||
#include <compiler/utils/fzl_ctable.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
// Type of context
|
||||
typedef enum
|
||||
{
|
||||
FZL_WALK_CTX_TYPE_GLOBAL,
|
||||
FZL_WALK_CTX_TYPE_FN,
|
||||
FZL_WALK_CTX_TYPE_CLASS,
|
||||
FZL_WALK_CTX_TYPE_ASSIGN
|
||||
} fzl_walk_ctx_type_t;
|
||||
|
||||
// Internal walk function result
|
||||
typedef struct
|
||||
{
|
||||
@@ -16,11 +26,16 @@ typedef struct
|
||||
|
||||
char *error_msg;
|
||||
bool error;
|
||||
|
||||
char *classname;
|
||||
} fzl_walk_res_t;
|
||||
|
||||
// Function context descriptor
|
||||
typedef struct
|
||||
typedef struct fzl_walk_ctx
|
||||
{
|
||||
fzl_walk_ctx_type_t type;
|
||||
struct fzl_walk_ctx *parent;
|
||||
|
||||
fzl_node_t *fn;
|
||||
size_t initial_env_lvl;
|
||||
} fzl_walk_ctx_t;
|
||||
@@ -39,6 +54,9 @@ typedef struct
|
||||
size_t sym_top;
|
||||
|
||||
fzl_walk_ctx_t *ctx;
|
||||
|
||||
fzl_ctable_t *classes[64];
|
||||
size_t classes_count;
|
||||
} fzl_walker_t;
|
||||
|
||||
// Allocate new walker
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
#ifndef FZL_CTABLE_H
|
||||
#define FZL_CTABLE_H
|
||||
|
||||
#include <common/fzl_types.h>
|
||||
#include <stddef.h>
|
||||
|
||||
// Class field descriptor
|
||||
typedef struct
|
||||
{
|
||||
char *name;
|
||||
size_t slot_index;
|
||||
fzl_type_t type;
|
||||
} fzl_ctable_field_t;
|
||||
|
||||
// Class table descriptor
|
||||
typedef struct
|
||||
{
|
||||
char *name;
|
||||
fzl_ctable_field_t *fields;
|
||||
size_t fields_count;
|
||||
size_t fields_remainder;
|
||||
} fzl_ctable_t;
|
||||
|
||||
// Allocate class table
|
||||
fzl_ctable_t *fzl_mkctable(char *name);
|
||||
|
||||
// Free class table
|
||||
void fzl_freectable(fzl_ctable_t *ct);
|
||||
|
||||
#endif // FZL_CTABLE_H
|
||||
@@ -10,7 +10,8 @@ typedef enum
|
||||
{
|
||||
FZL_SYMTABLE_RESULT_OK,
|
||||
FZL_SYMTABLE_RESULT_ERR,
|
||||
FZL_SYMTABLE_RESULT_EXISTS
|
||||
FZL_SYMTABLE_RESULT_EXISTS,
|
||||
FZL_SYMTABLE_RESULT_NOT_OBJECT
|
||||
} fzl_symtable_res_t;
|
||||
|
||||
// Symbols table entry descriptor
|
||||
@@ -19,6 +20,8 @@ typedef struct
|
||||
char *name;
|
||||
fzl_type_t type;
|
||||
size_t ip;
|
||||
|
||||
char *classname;
|
||||
} fzl_sym_t;
|
||||
|
||||
// Symbols table descriptor
|
||||
@@ -37,6 +40,10 @@ fzl_symtable_t *fzl_mksymt();
|
||||
fzl_symtable_res_t fzl_symt_put(fzl_symtable_t *symt, char *name,
|
||||
fzl_type_t type, uint64_t ip);
|
||||
|
||||
// Assign class name to object type
|
||||
fzl_symtable_res_t fzl_symt_setobjclass(fzl_symtable_t *symt, char *name,
|
||||
char *classname);
|
||||
|
||||
// Get entry from specified symtable
|
||||
bool fzl_symt_get(fzl_symtable_t *symt, char *name, fzl_sym_t *res);
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <vm/fzl_vm_env.h>
|
||||
#include <vm/fzl_vm_object.h>
|
||||
|
||||
// VM descriptor
|
||||
typedef struct
|
||||
@@ -29,6 +30,9 @@ typedef struct
|
||||
// Environment stack
|
||||
fzl_vm_env_t *estack[256];
|
||||
size_t etop;
|
||||
|
||||
fzl_vm_object_t *objects[256];
|
||||
size_t objects_count;
|
||||
} fzl_vm_t;
|
||||
|
||||
// Allocate FZL VM
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
#ifndef FZL_VM_OBJECT_H
|
||||
#define FZL_VM_OBJECT_H
|
||||
|
||||
#include <common/fzl_value.h>
|
||||
#include <stddef.h>
|
||||
|
||||
// Object descriptor
|
||||
typedef struct
|
||||
{
|
||||
fzl_value_t *values;
|
||||
size_t values_count;
|
||||
} fzl_vm_object_t;
|
||||
|
||||
#endif // FZL_VM_OBJECT_H
|
||||
Reference in New Issue
Block a user