Compare commits
21
Commits
92ec929e9f
...
c0dc14a0c7
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c0dc14a0c7 | ||
|
|
7e52c671aa | ||
|
|
8043c30d9d | ||
|
|
b8769ac81c | ||
|
|
d86d95f4fa | ||
|
|
e7573743aa | ||
|
|
2c76277a68 | ||
|
|
01afe2e972 | ||
|
|
83e20d78f3 | ||
|
|
e3b8ec6b7d | ||
|
|
028c8270f7 | ||
|
|
e01bf6e4ab | ||
|
|
96cd855c5d | ||
|
|
8696d5bfe7 | ||
|
|
4577b541c3 | ||
|
|
b9c43c6ad2 | ||
|
|
981603105b | ||
|
|
a815cd1549 | ||
|
|
1db985ecb5 | ||
|
|
f49a54c878 | ||
|
|
3f76cdcefb |
@@ -5,3 +5,5 @@ BreakBeforeBraces: Allman
|
||||
IndentWidth: 2
|
||||
TabWidth: 2
|
||||
UseTab: Never
|
||||
AlignConsecutiveAssignments: Consecutive
|
||||
IndentCaseLabels: True
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
/*
|
||||
|
||||
# Dirs
|
||||
!/docs
|
||||
!/exl
|
||||
!/include
|
||||
!/src
|
||||
|
||||
# Files
|
||||
!/.clang-format
|
||||
!/.gdbinit
|
||||
!/.gitignore
|
||||
!/CMakeLists.txt
|
||||
!/Makefile
|
||||
!/README.md
|
||||
!/README.ru.md
|
||||
|
||||
|
||||
@@ -15,10 +15,13 @@ add_executable(fzc
|
||||
src/compiler/fzl_walker.c
|
||||
src/compiler/fzl_writer.c
|
||||
src/compiler/utils/fzl_const_pool.c
|
||||
src/compiler/utils/fzl_symtable.c
|
||||
src/compiler/main.c)
|
||||
|
||||
# Virtual machine
|
||||
add_executable(fz
|
||||
src/vm/fzl_vm.c
|
||||
src/vm/fzl_vm_env.c
|
||||
src/vm/fzl_vm_debug.c
|
||||
src/vm/main.c)
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
.PHONY: configure build
|
||||
.PHONY: configure build debug-c debug-vm
|
||||
|
||||
configure:
|
||||
cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug
|
||||
@@ -6,3 +6,9 @@ configure:
|
||||
build: configure
|
||||
cmake --build build
|
||||
|
||||
debug-c: build
|
||||
gdb --args ./build/fzc ${FILE}
|
||||
|
||||
debug-vm: build
|
||||
gdb --args ./build/fz ${FILE}
|
||||
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
# FromZero / Language
|
||||
|
||||
[README in Russian](./README.ru.md)
|
||||
|
||||
**FromZero/Language** is a package consisting of a JIT compiler and a virtual machine for the FZL language.
|
||||
|
||||
## Features
|
||||
|
||||
At the moment, the language has the following capabilities:
|
||||
|
||||
- Builtin `printline` function for printing argument to stdout
|
||||
- String and numeric literals
|
||||
- Arithmetic operations: addition, subtraction, multiplication, division, modulo, power
|
||||
|
||||
|
||||
## File extensions
|
||||
|
||||
Standard file extensions:
|
||||
|
||||
- `.fzs` - source code
|
||||
- `.fzb` - compiled binary file
|
||||
|
||||
|
||||
## Building
|
||||
|
||||
1. Clone the repository: `git clone https://git.samsonium.dev/samsonium/fz-lang.git`
|
||||
2. Build: `make build`
|
||||
|
||||
After this, a "build" directory will appear in the project root. It will contain two executable files:
|
||||
|
||||
- `fzc` - a compiler
|
||||
- `fz` - virtual machine
|
||||
|
||||
Both executables have usage help that can be displayed using `--help` flag (or `-h`).
|
||||
|
||||
|
||||
## Development guideline
|
||||
|
||||
You need a code editor with Clang LSP support because of `.clang-format` file in the project root.
|
||||
This file provides set up codestyle rules.
|
||||
|
||||
There is two targets in Makefile for debugging purposes: `build-c` for the compiler and `build-vm` for the VM.
|
||||
Each target can be called with `FILE` variable. For example:
|
||||
|
||||
|
||||
```shell
|
||||
$ make debug-c FILE=exl/hw.fzs
|
||||
```
|
||||
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
# FromZero / Language
|
||||
|
||||
[README in English](./README.md)
|
||||
|
||||
**FromZero/Language** - пакет из JIT-компилятора и виртуальной машины для языка FZL.
|
||||
|
||||
## Текущие возможности
|
||||
|
||||
На данный момент язык имеет:
|
||||
|
||||
- Встроенную функцию `printline` для вывода аргумента в stdout
|
||||
- Строковый и числовой литералы
|
||||
- Арифметические операции: сложение, вычитание, умножение, деление, модуло, возведение в степень
|
||||
|
||||
|
||||
## Расширения файлов
|
||||
|
||||
Стандартными для языка являются два расширения файлов:
|
||||
|
||||
- `.fzs` - исходный код
|
||||
- `.fzb` - скомпилированный бинарный файл
|
||||
|
||||
|
||||
## Инструкции по сборке
|
||||
|
||||
1. Склонировать проект: `git clone https://git.samsonium.dev/samsonium/fz-lang.git`
|
||||
2. Собрать проект: `make build`
|
||||
|
||||
После этого в корне появится директория **"build"**. В ней будут лежать два скомпилированных бинарных файла:
|
||||
|
||||
- `fzc` - компилятор
|
||||
- `fz` - виртуальная машина
|
||||
|
||||
Оба бинарных файла предоставляют справку по использованию при запуске с флагом `--help` (или `-h`).
|
||||
|
||||
|
||||
## Гайдлайн по разработке
|
||||
|
||||
Желательно иметь редактор кода, поддерживающий Clang LSP, так как в проекте есть готовые настройки для
|
||||
форматтера в виде файла `.clang-format`.
|
||||
|
||||
Для дебаггинга есть два таргета в CMake: `build-c` для компилятора и `build-vm` для виртуальной машины.
|
||||
Оба таргета позволяют указать переменную с путём файла. Например, так:
|
||||
|
||||
```shell
|
||||
$ make debug-c FILE=exl/hw.fzs
|
||||
```
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
program := statement*
|
||||
statement := expression
|
||||
expression := call | string_literal
|
||||
call := name "(" argument-list? ")"
|
||||
argument-list := string_literal ("," string_literal)*
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
printline(2 + 2 * 2)
|
||||
printline(-1 + 1)
|
||||
printline(2 ^ 2)
|
||||
printline(1 + 1 * 1 / 1)
|
||||
printline(4 % 2)
|
||||
printline(10 / 2)
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
printline("")
|
||||
@@ -0,0 +1 @@
|
||||
printline("hello)
|
||||
@@ -0,0 +1,13 @@
|
||||
uint work_it = 21 * 2 ^ 2
|
||||
int make_it = 10
|
||||
|
||||
u64 do_it
|
||||
do_it = 15
|
||||
|
||||
printline(work_it)
|
||||
printline(make_it + do_it)
|
||||
|
||||
char make_us
|
||||
str floyd = "hello, Pink Floyd!"
|
||||
printline(floyd)
|
||||
|
||||
@@ -4,9 +4,23 @@
|
||||
// Available opcodes
|
||||
typedef enum
|
||||
{
|
||||
FZL_OPCODE_LOADCONST, // Load const into T (temp) reg
|
||||
FZL_OPCODE_PUSHARG, // Push argument from T into args stack
|
||||
FZL_OPCODE_CALL_BUILTIN // Call builtin function
|
||||
FZL_OPCODE_LOADCONST = 0xF00001, // Load const into T (temp) reg
|
||||
FZL_OPCODE_CALL_BUILTIN = 0xF00002, // Call builtin function
|
||||
FZL_OPCODE_PUSHNUM = 0xF00003, // Push number into stack
|
||||
|
||||
FZL_OPCODE_MK_ENVTABLE = 0xF10001, // Allocate new environment table
|
||||
FZL_OPCODE_RM_ENVTABLE = 0xF10002, // Free environment table
|
||||
FZL_OPCODE_STORE = 0xF10003, // Push variable to environment table
|
||||
FZL_OPCODE_LOOKUP_ENV = 0xF10004, // Lookup name in environment table
|
||||
FZL_OPCODE_WRITE = 0xF10005, // Wirte into variable
|
||||
|
||||
FZL_OPCODE_ADD = 0xA00001, // Sum two numbers in arg strack
|
||||
FZL_OPCODE_SUB = 0xA00002, // Subract two numbers in arg stack
|
||||
FZL_OPCODE_MUL = 0xA00003, // Multiply two numbers in arg stack
|
||||
FZL_OPCODE_DIV = 0xA00004, // Divide two numbers in arg stack
|
||||
FZL_OPCODE_POW = 0xA00005, // Power first number in arg stack by second
|
||||
FZL_OPCODE_MOD = 0xA00006, // Get module of two numbers in arg stack
|
||||
FZL_OPCODE_NEG = 0xA00007, // Negotiate number in arg stack
|
||||
} fzl_opcode_t;
|
||||
|
||||
#endif // FZL_OPCODES_H
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
#ifndef FZL_TYPES_H
|
||||
#define FZL_TYPES_H
|
||||
|
||||
typedef enum
|
||||
{
|
||||
FZL_TYPE_NULL,
|
||||
|
||||
FZL_TYPE_STRING,
|
||||
FZL_TYPE_CHAR,
|
||||
FZL_TYPE_BOOL,
|
||||
|
||||
FZL_TYPE_INT8,
|
||||
FZL_TYPE_UINT8,
|
||||
FZL_TYPE_INT16,
|
||||
FZL_TYPE_UINT16,
|
||||
FZL_TYPE_INT32,
|
||||
FZL_TYPE_UINT32,
|
||||
FZL_TYPE_INT64,
|
||||
FZL_TYPE_UINT64,
|
||||
FZL_TYPE_INT, // machine word int
|
||||
FZL_TYPE_UINT // machine word unsigned int
|
||||
} fzl_type_t;
|
||||
|
||||
#endif // FZL_TYPES_H
|
||||
@@ -0,0 +1,42 @@
|
||||
#ifndef FZL_VALUE_H
|
||||
#define FZL_VALUE_H
|
||||
|
||||
#include <common/fzl_types.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
// Value container descriptor
|
||||
typedef struct
|
||||
{
|
||||
fzl_type_t type;
|
||||
|
||||
union
|
||||
{
|
||||
struct
|
||||
{
|
||||
const char *value;
|
||||
size_t length;
|
||||
} s;
|
||||
|
||||
char c;
|
||||
bool b;
|
||||
|
||||
int8_t i8;
|
||||
uint8_t u8;
|
||||
|
||||
int16_t i16;
|
||||
uint16_t u16;
|
||||
|
||||
int32_t i32;
|
||||
uint32_t u32;
|
||||
|
||||
int64_t i64;
|
||||
uint64_t u64;
|
||||
|
||||
intptr_t i;
|
||||
uintptr_t u;
|
||||
} as;
|
||||
} fzl_value_t;
|
||||
|
||||
#endif // FZL_VALUE_H
|
||||
@@ -11,6 +11,20 @@ typedef enum
|
||||
FZL_NODETYPE_BLOCK,
|
||||
FZL_NODETYPE_CALL,
|
||||
FZL_NODETYPE_LITERAL,
|
||||
FZL_NODETYPE_IDENTIFIER,
|
||||
|
||||
FZL_NODETYPE_ADD,
|
||||
FZL_NODETYPE_SUB,
|
||||
FZL_NODETYPE_MUL,
|
||||
FZL_NODETYPE_DIV,
|
||||
FZL_NODETYPE_POW,
|
||||
FZL_NODETYPE_MOD,
|
||||
|
||||
FZL_NODETYPE_UNARY_PLUS,
|
||||
FZL_NODETYPE_UNARY_MINUS,
|
||||
|
||||
FZL_NODETYPE_DECLARATION,
|
||||
FZL_NODETYPE_ASSIGN
|
||||
} fzl_nodetype_t;
|
||||
|
||||
// AST node descriptor
|
||||
@@ -23,11 +37,41 @@ typedef struct fzl_node
|
||||
|
||||
union
|
||||
{
|
||||
// Array of nested nodes (for blocks, etc)
|
||||
struct
|
||||
{
|
||||
struct fzl_node **entries;
|
||||
size_t entry_count;
|
||||
} list;
|
||||
|
||||
// Call data
|
||||
struct
|
||||
{
|
||||
struct fzl_node *callee;
|
||||
|
||||
struct fzl_node **args;
|
||||
size_t args_count;
|
||||
} call;
|
||||
|
||||
// Binary operations
|
||||
struct
|
||||
{
|
||||
struct fzl_node *left;
|
||||
struct fzl_node *right;
|
||||
} binary_op;
|
||||
|
||||
// Unary operations
|
||||
struct
|
||||
{
|
||||
struct fzl_node *operand;
|
||||
} unary_op;
|
||||
|
||||
// Variables declaration and initializing
|
||||
struct
|
||||
{
|
||||
fzl_token_t *type;
|
||||
struct fzl_node *identifier;
|
||||
} var;
|
||||
};
|
||||
} fzl_node_t;
|
||||
|
||||
|
||||
@@ -20,8 +20,11 @@ typedef struct
|
||||
fzl_reader_ll_t *last;
|
||||
|
||||
char *source;
|
||||
size_t size;
|
||||
|
||||
size_t position;
|
||||
bool at_end;
|
||||
size_t line_start;
|
||||
size_t line_n;
|
||||
} fzl_reader_t;
|
||||
|
||||
// Allocate new reader
|
||||
|
||||
@@ -8,26 +8,41 @@
|
||||
// Token type
|
||||
typedef enum
|
||||
{
|
||||
FZL_TOKTYPE_NAME,
|
||||
FZL_TOKTYPE_LPAIR,
|
||||
FZL_TOKTYPE_RPAIR,
|
||||
FZL_TOKTYPE_COMMA,
|
||||
FZL_TOKTYPE_STR_LITERAL
|
||||
FZL_TOKTYPE_NAME, // abc
|
||||
FZL_TOKTYPE_TYPE, // i16
|
||||
|
||||
FZL_TOKTYPE_LPAREN, // (
|
||||
FZL_TOKTYPE_RPAREN, // )
|
||||
|
||||
FZL_TOKTYPE_COMMA, // ,
|
||||
FZL_TOKTYPE_DOT, // .
|
||||
|
||||
FZL_TOKTYPE_STR_LITERAL, // ""
|
||||
FZL_TOKTYPE_NUM_LITERAL, // 123
|
||||
|
||||
FZL_TOKTYPE_STAR, // *
|
||||
FZL_TOKTYPE_SLASH, // /
|
||||
FZL_TOKTYPE_PLUS, // +
|
||||
FZL_TOKTYPE_MINUS, // -
|
||||
FZL_TOKTYPE_CARET, // ^
|
||||
FZL_TOKTYPE_PERCENT, // %
|
||||
|
||||
FZL_TOKTYPE_EQ // =
|
||||
} fzl_toktype_t;
|
||||
|
||||
// Token descriptor
|
||||
typedef struct
|
||||
{
|
||||
fzl_toktype_t type;
|
||||
uint16_t i0;
|
||||
uint16_t i1;
|
||||
size_t ln;
|
||||
size_t cn;
|
||||
bool store_value;
|
||||
void *value;
|
||||
uint16_t value_sz;
|
||||
} fzl_token_t;
|
||||
|
||||
// Allocate new token
|
||||
fzl_token_t *fzl_mktoken(const fzl_toktype_t type, uint16_t from, uint16_t to,
|
||||
fzl_token_t *fzl_mktoken(const fzl_toktype_t type, size_t line, size_t col,
|
||||
void *value, size_t value_sz);
|
||||
|
||||
// Free token and its contents
|
||||
|
||||
@@ -2,10 +2,22 @@
|
||||
#define FZL_WALKER_H
|
||||
|
||||
#include <common/fzl_opcodes.h>
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/utils/fzl_const_pool.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
// Internal walk function result
|
||||
typedef struct
|
||||
{
|
||||
fzl_type_t type;
|
||||
|
||||
char *error_msg;
|
||||
bool error;
|
||||
} fzl_walk_res_t;
|
||||
|
||||
// Walker descriptor
|
||||
typedef struct
|
||||
{
|
||||
@@ -15,6 +27,9 @@ typedef struct
|
||||
uint64_t *ops;
|
||||
size_t op_count;
|
||||
size_t op_remainder;
|
||||
|
||||
fzl_symtable_t *syms[64];
|
||||
size_t sym_top;
|
||||
} fzl_walker_t;
|
||||
|
||||
// Allocate new walker
|
||||
|
||||
@@ -8,7 +8,8 @@
|
||||
// Constant types
|
||||
typedef enum
|
||||
{
|
||||
FZL_CONSTTYPE_STRING
|
||||
FZL_CONSTTYPE_STRING,
|
||||
FZL_CONSTTYPE_NUMBER
|
||||
} fzl_consttype_t;
|
||||
|
||||
// Constant descriptor
|
||||
@@ -23,6 +24,11 @@ typedef struct
|
||||
const char *chars;
|
||||
size_t length;
|
||||
} string;
|
||||
|
||||
struct
|
||||
{
|
||||
uint64_t value;
|
||||
} num;
|
||||
} as;
|
||||
} fzl_const_t;
|
||||
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
#ifndef FZL_SYMTABLE_H
|
||||
#define FZL_SYMTABLE_H
|
||||
|
||||
#include <common/fzl_types.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
typedef enum
|
||||
{
|
||||
FZL_SYMTABLE_RESULT_OK,
|
||||
FZL_SYMTABLE_RESULT_ERR,
|
||||
FZL_SYMTABLE_RESULT_EXISTS
|
||||
} fzl_symtable_res_t;
|
||||
|
||||
// Symbols table entry descriptor
|
||||
typedef struct
|
||||
{
|
||||
char *name;
|
||||
fzl_type_t type;
|
||||
} fzl_sym_t;
|
||||
|
||||
// Symbols table descriptor
|
||||
typedef struct
|
||||
{
|
||||
size_t size;
|
||||
size_t remainder;
|
||||
|
||||
fzl_sym_t *symbols;
|
||||
} fzl_symtable_t;
|
||||
|
||||
// Allocate new symbtable
|
||||
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);
|
||||
|
||||
// Get entry from specified symtable
|
||||
bool fzl_symt_get(fzl_symtable_t *symt, char *name, fzl_sym_t *res);
|
||||
|
||||
// Free symbtable
|
||||
void fzl_freesymt(fzl_symtable_t *symt);
|
||||
|
||||
#endif // FZL_SYMTABLE_H
|
||||
+15
-4
@@ -1,9 +1,11 @@
|
||||
#ifndef FZL_VM_H
|
||||
#define FZL_VM_H
|
||||
|
||||
#include <common/fzl_value.h>
|
||||
#include <compiler/utils/fzl_const_pool.h> // NOTE: move outside compiler scope to common
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <vm/fzl_vm_env.h>
|
||||
|
||||
// VM descriptor
|
||||
typedef struct
|
||||
@@ -16,11 +18,17 @@ typedef struct
|
||||
|
||||
size_t ip;
|
||||
|
||||
fzl_const_t stack[256];
|
||||
size_t stack_top;
|
||||
// Data stack
|
||||
fzl_value_t dstack[256];
|
||||
size_t dtop;
|
||||
|
||||
fzl_const_t arg_stack[256];
|
||||
size_t arg_top;
|
||||
// Call stack
|
||||
size_t cstack[256];
|
||||
size_t ctop;
|
||||
|
||||
// Environment stack
|
||||
fzl_vm_env_t *estack[256];
|
||||
size_t etop;
|
||||
} fzl_vm_t;
|
||||
|
||||
// Allocate FZL VM
|
||||
@@ -32,6 +40,9 @@ int fzl_vm_run(fzl_vm_t *vm);
|
||||
// Get last VM error message
|
||||
char *fzl_vm_geterror();
|
||||
|
||||
// Print opcodes sequence
|
||||
void fzl_vm_print_ops(fzl_vm_t *vm);
|
||||
|
||||
// Free VM
|
||||
void fzl_freevm(fzl_vm_t *vm);
|
||||
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
#ifndef FZL_VM_ENV_H
|
||||
#define FZL_VM_ENV_H
|
||||
|
||||
#include <common/fzl_value.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
typedef enum
|
||||
{
|
||||
FZL_VM_ENV_WRESULT_OK,
|
||||
FZL_VM_ENV_WRESULT_ERR,
|
||||
FZL_VM_ENV_WRESULT_ERR_EXISTS,
|
||||
FZL_VM_ENV_WRESULT_ERR_NOT_FOUND
|
||||
} fzl_vm_env_wresult_t;
|
||||
|
||||
// Environment table entry descriptor
|
||||
typedef struct
|
||||
{
|
||||
char *name;
|
||||
fzl_value_t value;
|
||||
} fzl_vm_env_entry_t;
|
||||
|
||||
// Environment table descriptor
|
||||
typedef struct
|
||||
{
|
||||
size_t size;
|
||||
size_t remainder;
|
||||
|
||||
fzl_vm_env_entry_t *table;
|
||||
} fzl_vm_env_t;
|
||||
|
||||
// Allocate env table
|
||||
fzl_vm_env_t *fzl_mkenv();
|
||||
|
||||
// Try get value by name
|
||||
fzl_value_t fzl_env_lookup(fzl_vm_env_t *env, char *name);
|
||||
|
||||
// Check name in env table
|
||||
bool fzl_env_contains(fzl_vm_env_t *env, char *name);
|
||||
|
||||
// Store new value
|
||||
fzl_vm_env_wresult_t fzl_env_storevalue(fzl_vm_env_t *env, char *name,
|
||||
fzl_value_t val);
|
||||
|
||||
// Overwrite existing value
|
||||
fzl_vm_env_wresult_t fzl_env_writevalue(fzl_vm_env_t *env, char *name,
|
||||
fzl_value_t newval);
|
||||
|
||||
// Free env table
|
||||
void fzl_freeenv(fzl_vm_env_t *env);
|
||||
|
||||
#endif // FZL_VM_ENV_H
|
||||
@@ -1,3 +1,4 @@
|
||||
#include "compiler/fzl_parser.h"
|
||||
#include <compiler/fzl_debug.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
@@ -31,13 +32,27 @@ void print_node(fzl_node_t *node, int level)
|
||||
|
||||
switch (node->type)
|
||||
{
|
||||
case FZL_NODETYPE_DECLARATION:
|
||||
printf("%s Declare variable with type %d:\n", levelbuf,
|
||||
node->var.type->type);
|
||||
print_node(node->var.identifier, level + 1);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_ASSIGN:
|
||||
printf("%s Assign:\n", levelbuf);
|
||||
printf("-%s Target:\n", levelbuf);
|
||||
print_node(node->binary_op.left, level + 2);
|
||||
printf("-%s Source:\n", levelbuf);
|
||||
print_node(node->binary_op.right, level + 2);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_CALL:
|
||||
if (node->list.entry_count > 0)
|
||||
if (node->call.args_count > 0)
|
||||
{
|
||||
printf("%s Call fn '%s' with args:\n", levelbuf,
|
||||
(char *)node->token->value);
|
||||
for (size_t i = 0; i < node->list.entry_count; i++)
|
||||
print_node(node->list.entries[i], level + 1);
|
||||
for (size_t i = 0; i < node->call.args_count; i++)
|
||||
print_node(node->call.args[i], level + 1);
|
||||
}
|
||||
else
|
||||
printf("%s Call fn '%s' w/o args\n", levelbuf,
|
||||
@@ -62,6 +77,22 @@ void print_node(fzl_node_t *node, int level)
|
||||
print_node(node->list.entries[i], level + 1);
|
||||
|
||||
break;
|
||||
case FZL_NODETYPE_ADD:
|
||||
case FZL_NODETYPE_SUB:
|
||||
case FZL_NODETYPE_MUL:
|
||||
case FZL_NODETYPE_DIV:
|
||||
case FZL_NODETYPE_POW:
|
||||
case FZL_NODETYPE_MOD:
|
||||
printf("%s Binary op (%d)\n", levelbuf, node->type);
|
||||
printf("-%s LEFT:\n", levelbuf);
|
||||
print_node(node->binary_op.left, level + 2);
|
||||
printf("-%s RIGHT:\n", levelbuf);
|
||||
print_node(node->binary_op.right, level + 2);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_IDENTIFIER:
|
||||
printf("%s Identifier ('%s')\n", levelbuf, (char *)node->token->value);
|
||||
break;
|
||||
|
||||
default:
|
||||
printf("%s Secure node\n", levelbuf);
|
||||
|
||||
+375
-57
@@ -1,6 +1,10 @@
|
||||
#include "compiler/fzl_token.h"
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char plast_err[256];
|
||||
|
||||
@@ -44,7 +48,7 @@ fzl_node_t *fzl_mknode(fzl_nodetype_t type, fzl_token_t *token)
|
||||
// Get token at cursor
|
||||
static fzl_token_t *peek(fzl_parser_t *parser)
|
||||
{
|
||||
if (!parser->cursor)
|
||||
if (!parser || !parser->cursor)
|
||||
return NULL;
|
||||
return parser->cursor->token;
|
||||
}
|
||||
@@ -52,7 +56,7 @@ static fzl_token_t *peek(fzl_parser_t *parser)
|
||||
// Get token at (cursor + 1)
|
||||
static fzl_token_t *next(fzl_parser_t *parser)
|
||||
{
|
||||
if (!parser->cursor)
|
||||
if (!parser || !parser->cursor)
|
||||
return NULL;
|
||||
|
||||
fzl_reader_ll_t *next = parser->cursor->next;
|
||||
@@ -65,97 +69,385 @@ static fzl_token_t *next(fzl_parser_t *parser)
|
||||
// Consume token at cursor and move to the next
|
||||
static fzl_token_t *consume(fzl_parser_t *parser)
|
||||
{
|
||||
if (!parser->cursor)
|
||||
if (!parser || !parser->cursor)
|
||||
return NULL;
|
||||
fzl_token_t *tok = parser->cursor->token;
|
||||
parser->cursor = parser->cursor->next;
|
||||
return tok;
|
||||
}
|
||||
|
||||
// Check if current token have the same type as specified
|
||||
static bool is(fzl_parser_t *parser, fzl_toktype_t type)
|
||||
{
|
||||
if (!parser)
|
||||
return false;
|
||||
|
||||
fzl_token_t *tok = peek(parser);
|
||||
if (!tok)
|
||||
return false;
|
||||
|
||||
return tok->type == type;
|
||||
}
|
||||
|
||||
// Convert arithmetic operator token type to node type
|
||||
static fzl_nodetype_t math_op_to_type(fzl_toktype_t type)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case FZL_TOKTYPE_PLUS:
|
||||
return FZL_NODETYPE_ADD;
|
||||
case FZL_TOKTYPE_MINUS:
|
||||
return FZL_NODETYPE_SUB;
|
||||
case FZL_TOKTYPE_STAR:
|
||||
return FZL_NODETYPE_MUL;
|
||||
case FZL_TOKTYPE_SLASH:
|
||||
return FZL_NODETYPE_DIV;
|
||||
case FZL_TOKTYPE_CARET:
|
||||
return FZL_NODETYPE_POW;
|
||||
case FZL_TOKTYPE_PERCENT:
|
||||
return FZL_NODETYPE_MOD;
|
||||
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static fzl_nodetype_t unary_op_to_type(fzl_toktype_t type)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case FZL_TOKTYPE_PLUS:
|
||||
return FZL_NODETYPE_UNARY_PLUS;
|
||||
case FZL_TOKTYPE_MINUS:
|
||||
return FZL_NODETYPE_UNARY_MINUS;
|
||||
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
fzl_node_t *parse_expression(fzl_parser_t *parser);
|
||||
|
||||
// Parse primary
|
||||
fzl_node_t *parse_primary(fzl_parser_t *parser)
|
||||
{
|
||||
if (peek(parser) && peek(parser)->type == FZL_TOKTYPE_STR_LITERAL)
|
||||
if (!parser || !peek(parser))
|
||||
return NULL;
|
||||
|
||||
if (is(parser, FZL_TOKTYPE_STR_LITERAL))
|
||||
{
|
||||
fzl_token_t *tok = consume(parser);
|
||||
return fzl_mknode(FZL_NODETYPE_LITERAL, tok);
|
||||
}
|
||||
|
||||
snprintf(plast_err, sizeof(plast_err), "failed to parse primary at %d",
|
||||
peek(parser)->i0);
|
||||
else if (is(parser, FZL_TOKTYPE_NUM_LITERAL))
|
||||
{
|
||||
fzl_token_t *tok = consume(parser);
|
||||
return fzl_mknode(FZL_NODETYPE_LITERAL, tok);
|
||||
}
|
||||
else if (is(parser, FZL_TOKTYPE_LPAREN))
|
||||
{
|
||||
consume(parser);
|
||||
fzl_node_t *node = parse_expression(parser);
|
||||
if (!is(parser, FZL_TOKTYPE_RPAREN))
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err), "expected ')' at %ld:%ld",
|
||||
parser->cursor->token->ln, parser->cursor->token->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_node_t *parse_expression(fzl_parser_t *parser, int precedence);
|
||||
|
||||
// Parse call expression
|
||||
fzl_node_t *parse_call(fzl_parser_t *parser)
|
||||
{
|
||||
fzl_token_t *tok = consume(parser); // <NAME>
|
||||
consume(parser); // (
|
||||
|
||||
fzl_node_t *node = fzl_mknode(FZL_NODETYPE_CALL, tok);
|
||||
node->list.entry_count = 0;
|
||||
|
||||
size_t capacity = 4;
|
||||
node->list.entries = malloc(capacity * sizeof(fzl_node_t *));
|
||||
node->list.entry_count = 0;
|
||||
|
||||
if (peek(parser) && peek(parser)->type == FZL_TOKTYPE_RPAIR)
|
||||
{
|
||||
consume(parser);
|
||||
return node;
|
||||
}
|
||||
|
||||
while (peek(parser) && peek(parser)->type != FZL_TOKTYPE_RPAIR)
|
||||
else if (is(parser, FZL_TOKTYPE_NAME))
|
||||
{
|
||||
fzl_node_t *arg = parse_expression(parser, 0);
|
||||
if (!arg)
|
||||
return NULL;
|
||||
|
||||
if (node->list.entry_count == capacity)
|
||||
{
|
||||
capacity *= 2;
|
||||
node->list.entries =
|
||||
realloc(node->list.entries, capacity * sizeof(fzl_node_t *));
|
||||
fzl_token_t *name = consume(parser);
|
||||
return fzl_mknode(FZL_NODETYPE_IDENTIFIER, name);
|
||||
}
|
||||
|
||||
node->list.entries[node->list.entry_count++] = arg;
|
||||
snprintf(plast_err, sizeof(plast_err), "failed to parse primary at %ld:%ld",
|
||||
peek(parser)->ln, peek(parser)->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (peek(parser) && peek(parser)->type == FZL_TOKTYPE_COMMA)
|
||||
// Parse postfix expressions (parens, index access, dots)
|
||||
fzl_node_t *parse_postfix(fzl_parser_t *parser)
|
||||
{
|
||||
if (!parser)
|
||||
return NULL;
|
||||
|
||||
fzl_node_t *left = parse_primary(parser);
|
||||
fzl_node_t *right = NULL;
|
||||
|
||||
if (!left)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err), "failed to parse postfix at %ld:%ld",
|
||||
peek(parser)->ln, peek(parser)->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
while (is(parser, FZL_TOKTYPE_LPAREN))
|
||||
{
|
||||
consume(parser);
|
||||
else if (peek(parser) && peek(parser)->type != FZL_TOKTYPE_RPAIR)
|
||||
|
||||
size_t args_capacity = 2;
|
||||
size_t args_count = 0;
|
||||
fzl_node_t **args = malloc(args_capacity * sizeof(fzl_node_t *));
|
||||
|
||||
while (!is(parser, FZL_TOKTYPE_RPAREN))
|
||||
{
|
||||
fzl_node_t *arg = parse_expression(parser);
|
||||
if (!arg)
|
||||
{
|
||||
free(args);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (args_capacity <= args_count)
|
||||
{
|
||||
args_capacity *= 2;
|
||||
args = realloc(args, args_capacity * sizeof(fzl_node_t));
|
||||
}
|
||||
|
||||
args[args_count] = arg;
|
||||
args_count++;
|
||||
|
||||
if (is(parser, FZL_TOKTYPE_COMMA))
|
||||
consume(parser);
|
||||
}
|
||||
|
||||
fzl_token_t *rparen = consume(parser);
|
||||
if (!rparen || rparen->type != FZL_TOKTYPE_RPAREN)
|
||||
{
|
||||
free(args);
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"expected ')' after argument list at %ld:%ld", peek(parser)->ln,
|
||||
peek(parser)->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_node_t *node = fzl_mknode(FZL_NODETYPE_CALL, left->token);
|
||||
node->call.callee = left;
|
||||
node->call.args = args;
|
||||
node->call.args_count = args_count;
|
||||
|
||||
left = node;
|
||||
}
|
||||
|
||||
return left;
|
||||
}
|
||||
|
||||
// Parse unary operations
|
||||
fzl_node_t *parse_unary(fzl_parser_t *parser)
|
||||
{
|
||||
if (!parser)
|
||||
return NULL;
|
||||
|
||||
if (!is(parser, FZL_TOKTYPE_MINUS) && !is(parser, FZL_TOKTYPE_PLUS))
|
||||
return parse_postfix(parser);
|
||||
|
||||
fzl_token_t *op = consume(parser);
|
||||
fzl_node_t *operand = parse_unary(parser);
|
||||
|
||||
if (!operand)
|
||||
return NULL;
|
||||
|
||||
fzl_node_t *result = fzl_mknode(unary_op_to_type(op->type), op);
|
||||
result->unary_op.operand = operand;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
fzl_node_t *parse_power(fzl_parser_t *parser)
|
||||
{
|
||||
if (!parser)
|
||||
return NULL;
|
||||
|
||||
fzl_node_t *left = parse_unary(parser);
|
||||
fzl_node_t *right = NULL;
|
||||
|
||||
if (!left)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err), "failed to parse power at %ld:%ld",
|
||||
peek(parser)->ln, peek(parser)->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (is(parser, FZL_TOKTYPE_CARET))
|
||||
{
|
||||
fzl_token_t *caret = consume(parser);
|
||||
|
||||
right = parse_unary(parser);
|
||||
if (!right)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"expected ',' or ')' after arg list");
|
||||
"failed to parse right side of power at %ld:%ld",
|
||||
peek(parser)->ln, peek(parser)->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_node_t *node = fzl_mknode(FZL_NODETYPE_POW, caret);
|
||||
node->binary_op.left = left;
|
||||
node->binary_op.right = right;
|
||||
|
||||
left = node;
|
||||
}
|
||||
|
||||
consume(parser);
|
||||
return node;
|
||||
return left;
|
||||
}
|
||||
|
||||
// Parse FACTOR: multiplication and division
|
||||
fzl_node_t *parse_factor(fzl_parser_t *parser)
|
||||
{
|
||||
if (!parser)
|
||||
return NULL;
|
||||
|
||||
fzl_node_t *left = parse_power(parser);
|
||||
fzl_node_t *right = NULL;
|
||||
|
||||
if (!left)
|
||||
{
|
||||
if (!plast_err[0])
|
||||
snprintf(plast_err, sizeof(plast_err), "invalid expression at %ld:%ld",
|
||||
parser->cursor->token->ln, parser->cursor->token->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
while (is(parser, FZL_TOKTYPE_STAR) || is(parser, FZL_TOKTYPE_SLASH) ||
|
||||
is(parser, FZL_TOKTYPE_PERCENT))
|
||||
{
|
||||
fzl_token_t *op = consume(parser);
|
||||
|
||||
right = parse_factor(parser);
|
||||
if (!right)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"unfinished factor (mul/div) expression at %ld:%ld",
|
||||
left->token->ln, left->token->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_node_t *node = fzl_mknode(math_op_to_type(op->type), op);
|
||||
node->binary_op.left = left;
|
||||
node->binary_op.right = right;
|
||||
left = node;
|
||||
}
|
||||
|
||||
return left;
|
||||
}
|
||||
|
||||
// Parse TERM: addition and subtraction
|
||||
fzl_node_t *parse_term(fzl_parser_t *parser)
|
||||
{
|
||||
if (!parser)
|
||||
return NULL;
|
||||
|
||||
fzl_node_t *left = parse_factor(parser);
|
||||
fzl_node_t *right = NULL;
|
||||
|
||||
if (!left)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err), "invalid expression at %ld:%ld",
|
||||
parser->cursor->token->ln, parser->cursor->token->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
while (is(parser, FZL_TOKTYPE_PLUS) || is(parser, FZL_TOKTYPE_MINUS))
|
||||
{
|
||||
fzl_token_t *op = consume(parser);
|
||||
|
||||
right = parse_factor(parser);
|
||||
if (!right)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err),
|
||||
"unfinished term (add/sub) expression at %ld:%ld",
|
||||
left->token->ln, left->token->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_node_t *node = fzl_mknode(math_op_to_type(op->type), op);
|
||||
node->binary_op.left = left;
|
||||
node->binary_op.right = right;
|
||||
left = node;
|
||||
}
|
||||
|
||||
return left;
|
||||
}
|
||||
|
||||
// Expression parsing
|
||||
fzl_node_t *parse_expression(fzl_parser_t *parser, int precedence)
|
||||
fzl_node_t *parse_expression(fzl_parser_t *parser)
|
||||
{
|
||||
if (peek(parser) && peek(parser)->type == FZL_TOKTYPE_NAME)
|
||||
{
|
||||
if (next(parser) && next(parser)->type == FZL_TOKTYPE_LPAIR)
|
||||
return parse_call(parser);
|
||||
}
|
||||
else if (peek(parser) && peek(parser)->type == FZL_TOKTYPE_STR_LITERAL)
|
||||
return parse_primary(parser);
|
||||
|
||||
snprintf(plast_err, sizeof(plast_err), "failed to parse expression at %d",
|
||||
peek(parser)->i0);
|
||||
return NULL;
|
||||
return parse_term(parser);
|
||||
}
|
||||
|
||||
// Statement parsing
|
||||
fzl_node_t *parse_statement(fzl_parser_t *parser)
|
||||
{
|
||||
return parse_expression(parser, 0);
|
||||
if (is(parser, FZL_TOKTYPE_TYPE))
|
||||
{
|
||||
fzl_token_t *type = peek(parser);
|
||||
if (type->type == FZL_TOKTYPE_TYPE)
|
||||
consume(parser);
|
||||
|
||||
if (type->type == FZL_TOKTYPE_TYPE || next(parser)->type == FZL_TOKTYPE_EQ)
|
||||
{
|
||||
|
||||
fzl_node_t *identifier = parse_primary(parser);
|
||||
if (!identifier || identifier->type != FZL_NODETYPE_IDENTIFIER)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err), "expected identifier at %ld:%ld",
|
||||
type->ln, type->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_node_t *left = fzl_mknode(FZL_NODETYPE_DECLARATION, type);
|
||||
left->var.type = type;
|
||||
left->var.identifier = identifier;
|
||||
|
||||
if (!is(parser, FZL_TOKTYPE_EQ))
|
||||
return left;
|
||||
|
||||
fzl_node_t *right;
|
||||
while (is(parser, FZL_TOKTYPE_EQ))
|
||||
{
|
||||
fzl_token_t *asn = consume(parser);
|
||||
right = parse_expression(parser);
|
||||
if (!right)
|
||||
return NULL;
|
||||
|
||||
fzl_node_t *node = fzl_mknode(FZL_NODETYPE_ASSIGN, asn);
|
||||
node->binary_op.left = left;
|
||||
node->binary_op.right = right;
|
||||
|
||||
left = node;
|
||||
}
|
||||
|
||||
return left;
|
||||
}
|
||||
}
|
||||
|
||||
if (is(parser, FZL_TOKTYPE_NAME) && next(parser)->type == FZL_TOKTYPE_EQ)
|
||||
{
|
||||
fzl_token_t *name = consume(parser);
|
||||
fzl_node_t *left = fzl_mknode(FZL_NODETYPE_IDENTIFIER, name);
|
||||
|
||||
while (is(parser, FZL_TOKTYPE_EQ))
|
||||
{
|
||||
fzl_token_t *eq = consume(parser);
|
||||
|
||||
fzl_node_t *right = parse_expression(parser);
|
||||
if (!right)
|
||||
return NULL;
|
||||
|
||||
fzl_node_t *assn = fzl_mknode(FZL_NODETYPE_ASSIGN, eq);
|
||||
assn->binary_op.left = left;
|
||||
assn->binary_op.right = right;
|
||||
|
||||
left = assn;
|
||||
}
|
||||
|
||||
return left;
|
||||
}
|
||||
|
||||
return parse_expression(parser);
|
||||
}
|
||||
|
||||
// Internal parsing entry point
|
||||
@@ -184,7 +476,7 @@ fzl_node_t *parse_program(fzl_parser_t *parser)
|
||||
|
||||
if (!first)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err), "empty ;(");
|
||||
snprintf(plast_err, sizeof(plast_err), "nothing to parse >:(");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -220,7 +512,7 @@ void fzl_freeast(fzl_node_t *root)
|
||||
|
||||
while (root)
|
||||
{
|
||||
if (root->type == FZL_NODETYPE_BLOCK || root->type == FZL_NODETYPE_CALL)
|
||||
if (root->type == FZL_NODETYPE_BLOCK)
|
||||
{
|
||||
if (root->list.entries)
|
||||
{
|
||||
@@ -231,6 +523,32 @@ void fzl_freeast(fzl_node_t *root)
|
||||
free(root->list.entries);
|
||||
}
|
||||
}
|
||||
else if (root->type == FZL_NODETYPE_CALL)
|
||||
{
|
||||
if (root->call.args_count)
|
||||
{
|
||||
for (size_t i = 0; i < root->call.args_count; i++)
|
||||
if (root->call.args[i])
|
||||
fzl_freeast(root->call.args[i]);
|
||||
|
||||
free(root->call.args);
|
||||
}
|
||||
}
|
||||
else if (root->type == FZL_NODETYPE_ADD || root->type == FZL_NODETYPE_SUB ||
|
||||
root->type == FZL_NODETYPE_MUL || root->type == FZL_NODETYPE_DIV ||
|
||||
root->type == FZL_NODETYPE_POW || root->type == FZL_NODETYPE_MOD)
|
||||
{
|
||||
if (root->binary_op.left)
|
||||
fzl_freeast(root->binary_op.left);
|
||||
if (root->binary_op.right)
|
||||
fzl_freeast(root->binary_op.right);
|
||||
}
|
||||
else if (root->type == FZL_NODETYPE_UNARY_PLUS ||
|
||||
root->type == FZL_NODETYPE_UNARY_MINUS)
|
||||
{
|
||||
if (root->unary_op.operand)
|
||||
fzl_freeast(root->unary_op.operand);
|
||||
}
|
||||
|
||||
fzl_freenode(root);
|
||||
root = root->next;
|
||||
|
||||
+132
-43
@@ -1,4 +1,7 @@
|
||||
#include "compiler/fzl_token.h"
|
||||
#include <compiler/fzl_reader.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -16,14 +19,35 @@ fzl_reader_t *fzl_mkreader(char *source)
|
||||
reader->first = first_node;
|
||||
reader->last = first_node;
|
||||
reader->source = source;
|
||||
reader->size = strlen(source);
|
||||
reader->position = 0;
|
||||
reader->at_end = true;
|
||||
reader->line_n = 1;
|
||||
reader->line_start = 0;
|
||||
|
||||
return reader;
|
||||
}
|
||||
|
||||
// --- Read zone ---
|
||||
|
||||
// Move cursor forward
|
||||
static size_t move(fzl_reader_t *reader, size_t len)
|
||||
{
|
||||
if (!reader)
|
||||
return 0;
|
||||
|
||||
char *src = reader->source;
|
||||
for (size_t i = 0; i < len; i++)
|
||||
{
|
||||
if (src[reader->position + i] == '\n')
|
||||
{
|
||||
reader->line_n++;
|
||||
reader->line_start = reader->position + i + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return reader->position += len;
|
||||
}
|
||||
|
||||
// Try parse name
|
||||
fzl_token_t *parse_name(fzl_reader_t *reader)
|
||||
{
|
||||
@@ -35,16 +59,30 @@ fzl_token_t *parse_name(fzl_reader_t *reader)
|
||||
lpos++;
|
||||
size++;
|
||||
} while ((reader->source[lpos] >= 'A' && reader->source[lpos] <= 'Z') ||
|
||||
(reader->source[lpos] >= 'a' && reader->source[lpos] <= 'z'));
|
||||
(reader->source[lpos] >= 'a' && reader->source[lpos] <= 'z') ||
|
||||
(reader->source[lpos] >= '0' && reader->source[lpos] <= '9') ||
|
||||
(reader->source[lpos] == '_'));
|
||||
|
||||
char *value = malloc(size);
|
||||
char *value = malloc(size + 1);
|
||||
memcpy(value, reader->source + reader->position, size);
|
||||
value[size] = '\0';
|
||||
|
||||
size_t start = reader->position;
|
||||
reader->position += size;
|
||||
move(reader, size);
|
||||
|
||||
fzl_token_t *tok =
|
||||
fzl_mktoken(FZL_TOKTYPE_NAME, start, reader->position - 1, value, size);
|
||||
fzl_token_t *tok;
|
||||
size_t line = reader->line_n;
|
||||
size_t pos = start - reader->line_start;
|
||||
|
||||
if (strcmp(value, "str") == 0 || strcmp(value, "char") == 0 ||
|
||||
strcmp(value, "uint") == 0 || strcmp(value, "int") == 0 ||
|
||||
strcmp(value, "i8") == 0 || strcmp(value, "i16") == 0 ||
|
||||
strcmp(value, "i32") == 0 || strcmp(value, "i64") == 0 ||
|
||||
strcmp(value, "u8") == 0 || strcmp(value, "u16") == 0 ||
|
||||
strcmp(value, "u32") == 0 || strcmp(value, "u64") == 0)
|
||||
tok = fzl_mktoken(FZL_TOKTYPE_TYPE, line, pos, value, size);
|
||||
else
|
||||
tok = fzl_mktoken(FZL_TOKTYPE_NAME, line, pos, value, size);
|
||||
free(value);
|
||||
|
||||
return tok;
|
||||
@@ -60,36 +98,86 @@ fzl_token_t *parse_string_literal(fzl_reader_t *reader)
|
||||
return NULL;
|
||||
|
||||
char open_symbol = reader->source[lpos];
|
||||
char close_symbol = '\0';
|
||||
|
||||
do
|
||||
{
|
||||
lpos++;
|
||||
size++;
|
||||
} while (reader->source[lpos] != open_symbol);
|
||||
|
||||
if (reader->source[lpos] == open_symbol)
|
||||
close_symbol = open_symbol;
|
||||
|
||||
} while (reader->source[lpos] != open_symbol && lpos < reader->size);
|
||||
size++;
|
||||
|
||||
if (close_symbol != open_symbol)
|
||||
{
|
||||
snprintf(last_err, sizeof(last_err), "unterminated string at %ld:%ld",
|
||||
reader->line_n, reader->position - reader->line_start);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (size == 2)
|
||||
{
|
||||
fzl_token_t *tok = fzl_mktoken(FZL_TOKTYPE_STR_LITERAL, reader->position,
|
||||
reader->position + 1, "", 0);
|
||||
fzl_token_t *tok =
|
||||
fzl_mktoken(FZL_TOKTYPE_STR_LITERAL, reader->line_n,
|
||||
reader->position - reader->line_start, "", 0);
|
||||
move(reader, 2);
|
||||
return tok;
|
||||
}
|
||||
|
||||
char *value = malloc(size - 2);
|
||||
memcpy(value, reader->source + reader->position + 1, size - 2);
|
||||
fzl_token_t *tok = fzl_mktoken(FZL_TOKTYPE_STR_LITERAL, reader->position,
|
||||
lpos, value, size - 2);
|
||||
fzl_token_t *tok = fzl_mktoken(FZL_TOKTYPE_STR_LITERAL, reader->line_n,
|
||||
lpos - reader->line_start, value, size - 2);
|
||||
free(value);
|
||||
|
||||
reader->position += size;
|
||||
move(reader, size);
|
||||
|
||||
return tok;
|
||||
}
|
||||
|
||||
// Parse numeric literal
|
||||
fzl_token_t *parse_number(fzl_reader_t *reader)
|
||||
{
|
||||
if (!reader)
|
||||
return NULL;
|
||||
|
||||
size_t size = 0;
|
||||
size_t lpos = reader->position;
|
||||
|
||||
do
|
||||
{
|
||||
size++;
|
||||
lpos++;
|
||||
} while ((reader->source[lpos] >= '0' && reader->source[lpos] <= '9') &&
|
||||
lpos < reader->size);
|
||||
|
||||
char *value = malloc(size * sizeof(char) + 1);
|
||||
memcpy(value, reader->source + reader->position, size);
|
||||
value[size] = '\0';
|
||||
|
||||
fzl_token_t *tok = fzl_mktoken(FZL_TOKTYPE_NUM_LITERAL, reader->line_n,
|
||||
lpos - reader->line_start, value, size);
|
||||
free(value);
|
||||
|
||||
if (!tok)
|
||||
return NULL;
|
||||
|
||||
move(reader, size);
|
||||
return tok;
|
||||
}
|
||||
|
||||
// Add single-char token
|
||||
fzl_token_t *add_single(fzl_reader_t *reader, fzl_toktype_t type)
|
||||
{
|
||||
size_t p = move(reader, 1);
|
||||
return fzl_mktoken(type, reader->line_n, p - reader->line_start, NULL, 0);
|
||||
}
|
||||
|
||||
int fzl_read(fzl_reader_t *reader)
|
||||
{
|
||||
const size_t len = strlen(reader->source);
|
||||
if (reader->position >= len)
|
||||
if (reader->position >= reader->size)
|
||||
return 0;
|
||||
|
||||
do
|
||||
@@ -100,47 +188,49 @@ int fzl_read(fzl_reader_t *reader)
|
||||
|
||||
if (curr == '\n' || curr == ' ' || curr == '\r')
|
||||
{
|
||||
reader->position++;
|
||||
move(reader, 1);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (curr == '\'' || curr == '"')
|
||||
token = parse_string_literal(reader);
|
||||
else if (curr == '(')
|
||||
{
|
||||
token = fzl_mktoken(FZL_TOKTYPE_LPAIR, p, p, NULL, 0);
|
||||
reader->position++;
|
||||
}
|
||||
token = add_single(reader, FZL_TOKTYPE_LPAREN);
|
||||
else if (curr == ')')
|
||||
{
|
||||
token = fzl_mktoken(FZL_TOKTYPE_RPAIR, p, p, NULL, 0);
|
||||
reader->position++;
|
||||
}
|
||||
else if (curr >= 'A' && curr <= 'Z' || curr >= 'a' && curr <= 'z')
|
||||
token = add_single(reader, FZL_TOKTYPE_RPAREN);
|
||||
else if (curr == ',')
|
||||
token = add_single(reader, FZL_TOKTYPE_COMMA);
|
||||
else if (curr == '*')
|
||||
token = add_single(reader, FZL_TOKTYPE_STAR);
|
||||
else if (curr == '/')
|
||||
token = add_single(reader, FZL_TOKTYPE_SLASH);
|
||||
else if (curr == '+')
|
||||
token = add_single(reader, FZL_TOKTYPE_PLUS);
|
||||
else if (curr == '-')
|
||||
token = add_single(reader, FZL_TOKTYPE_MINUS);
|
||||
else if (curr == '^')
|
||||
token = add_single(reader, FZL_TOKTYPE_CARET);
|
||||
else if (curr == '%')
|
||||
token = add_single(reader, FZL_TOKTYPE_PERCENT);
|
||||
else if (curr == '=')
|
||||
token = add_single(reader, FZL_TOKTYPE_EQ);
|
||||
else if (curr >= 'A' && curr <= 'Z' || curr >= 'a' && curr <= 'z' ||
|
||||
curr == '_')
|
||||
token = parse_name(reader);
|
||||
else if (curr >= '0' && curr <= '9')
|
||||
token = parse_number(reader);
|
||||
else
|
||||
{
|
||||
sprintf(last_err, "undefined token '%c'", curr);
|
||||
sprintf(last_err, "undefined token '%c' at %ld:%ld", curr, reader->line_n,
|
||||
p - reader->line_start);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!token)
|
||||
{
|
||||
size_t line = 1, linepos = 1;
|
||||
for (size_t i = 0; i < p; i++)
|
||||
{
|
||||
char sym = reader->source[i];
|
||||
if (sym == '\n')
|
||||
{
|
||||
linepos = 1;
|
||||
line++;
|
||||
}
|
||||
else
|
||||
linepos++;
|
||||
}
|
||||
|
||||
sprintf(last_err, "error parsing token (position %ld of line %ld)", line,
|
||||
linepos);
|
||||
if (!last_err[0])
|
||||
sprintf(last_err, "cannot parse token at %ld:%ld", reader->line_n,
|
||||
p - reader->line_start);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -149,9 +239,8 @@ int fzl_read(fzl_reader_t *reader)
|
||||
|
||||
reader->last->next = tok_node;
|
||||
reader->last = tok_node;
|
||||
} while (reader->position < len);
|
||||
} while (reader->position < reader->size);
|
||||
|
||||
reader->at_end = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,17 +1,19 @@
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
fzl_token_t *fzl_mktoken(const fzl_toktype_t type, uint16_t from, uint16_t to,
|
||||
fzl_token_t *fzl_mktoken(const fzl_toktype_t type, size_t line, size_t col,
|
||||
void *value, size_t value_len)
|
||||
{
|
||||
void *valholder = malloc(value_len);
|
||||
char *valholder = malloc(value_len + 1);
|
||||
memcpy(valholder, value, value_len);
|
||||
valholder[value_len] = '\0';
|
||||
|
||||
fzl_token_t *token = malloc(sizeof(fzl_token_t));
|
||||
token->type = type;
|
||||
token->i0 = from;
|
||||
token->i1 = to;
|
||||
token->ln = line;
|
||||
token->cn = col;
|
||||
token->store_value = value_len > 0;
|
||||
token->value = valholder;
|
||||
token->value_sz = value_len;
|
||||
|
||||
+295
-16
@@ -1,8 +1,11 @@
|
||||
#include <common/fzl_builtins.h>
|
||||
#include <common/fzl_opcodes.h>
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <compiler/fzl_walker.h>
|
||||
#include <compiler/utils/fzl_const_pool.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
@@ -41,11 +44,63 @@ fzl_walker_t *fzl_mkwalker(fzl_node_t *root)
|
||||
walker->op_remainder = 2;
|
||||
walker->ops = malloc(walker->op_remainder * sizeof(uint64_t));
|
||||
|
||||
walker->sym_top = 0;
|
||||
walker->syms[walker->sym_top++] = fzl_mksymt();
|
||||
if (!walker->syms[walker->sym_top - 1])
|
||||
{
|
||||
snprintf(wlast_err, sizeof(wlast_err), "failed to create global symtable");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return walker;
|
||||
}
|
||||
|
||||
// --- Walking zone ---
|
||||
|
||||
// Convert type token to `fzl_type_t`
|
||||
const fzl_type_t tok_to_type(fzl_token_t *tok)
|
||||
{
|
||||
if (strcmp((char *)tok->value, "i8") == 0)
|
||||
return FZL_TYPE_INT8;
|
||||
if (strcmp((char *)tok->value, "i16") == 0)
|
||||
return FZL_TYPE_INT16;
|
||||
if (strcmp((char *)tok->value, "i32") == 0)
|
||||
return FZL_TYPE_INT32;
|
||||
if (strcmp((char *)tok->value, "i64") == 0)
|
||||
return FZL_TYPE_INT64;
|
||||
|
||||
if (strcmp((char *)tok->value, "u8") == 0)
|
||||
return FZL_TYPE_UINT8;
|
||||
if (strcmp((char *)tok->value, "u16") == 0)
|
||||
return FZL_TYPE_UINT16;
|
||||
if (strcmp((char *)tok->value, "u32") == 0)
|
||||
return FZL_TYPE_UINT32;
|
||||
if (strcmp((char *)tok->value, "u64") == 0)
|
||||
return FZL_TYPE_UINT64;
|
||||
|
||||
if (strcmp((char *)tok->value, "int") == 0)
|
||||
return FZL_TYPE_INT;
|
||||
if (strcmp((char *)tok->value, "uint") == 0)
|
||||
return FZL_TYPE_UINT;
|
||||
|
||||
if (strcmp((char *)tok->value, "bool") == 0)
|
||||
return FZL_TYPE_BOOL;
|
||||
if (strcmp((char *)tok->value, "str") == 0)
|
||||
return FZL_TYPE_STRING;
|
||||
if (strcmp((char *)tok->value, "char") == 0)
|
||||
return FZL_TYPE_CHAR;
|
||||
|
||||
return FZL_TYPE_NULL;
|
||||
}
|
||||
|
||||
static bool is_compatible(fzl_type_t t1, fzl_type_t t2)
|
||||
{
|
||||
if (t1 == t2)
|
||||
return true;
|
||||
|
||||
return t1 >= FZL_TYPE_INT8 && t2 >= FZL_TYPE_INT8;
|
||||
}
|
||||
|
||||
// Add const to consts table
|
||||
size_t add_const(fzl_walker_t *walker, fzl_const_t c)
|
||||
{
|
||||
@@ -87,46 +142,94 @@ void push_word(fzl_walker_t *walker, uint64_t word)
|
||||
walker->ops[walker->op_count++] = word;
|
||||
}
|
||||
|
||||
bool walk(fzl_walker_t *walker, fzl_node_t *node)
|
||||
fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node)
|
||||
{
|
||||
if (!walker || !node)
|
||||
return false;
|
||||
return (fzl_walk_res_t){.error = true, .error_msg = "walker is null"};
|
||||
|
||||
fzl_walk_res_t current_res;
|
||||
while (node != NULL)
|
||||
{
|
||||
switch (node->type)
|
||||
{
|
||||
case FZL_NODETYPE_UNARY_PLUS:
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_IDENTIFIER:
|
||||
{
|
||||
size_t n = add_const(
|
||||
walker, (fzl_const_t){.type = FZL_CONSTTYPE_STRING,
|
||||
.as.string.chars = node->token->value,
|
||||
.as.string.length = node->token->value_sz});
|
||||
|
||||
fzl_sym_t sym;
|
||||
bool exists = fzl_symt_get(walker->syms[walker->sym_top - 1],
|
||||
node->token->value, &sym);
|
||||
if (!exists)
|
||||
{
|
||||
snprintf(wlast_err, sizeof(wlast_err),
|
||||
"cannot find symbol '%s' at %ld:%ld",
|
||||
(char *)node->token->value, node->token->ln,
|
||||
node->token->cn);
|
||||
return (fzl_walk_res_t){.error = true, .error_msg = wlast_err};
|
||||
}
|
||||
|
||||
push_op(walker, FZL_OPCODE_LOADCONST);
|
||||
push_word(walker, n);
|
||||
push_op(walker, FZL_OPCODE_LOOKUP_ENV);
|
||||
|
||||
current_res = (fzl_walk_res_t){.type = sym.type, .error = false};
|
||||
break;
|
||||
}
|
||||
|
||||
case FZL_NODETYPE_BLOCK:
|
||||
// NOTE: create block env here
|
||||
push_op(walker, FZL_OPCODE_MK_ENVTABLE);
|
||||
if (node->list.entry_count > 0)
|
||||
for (size_t i = 0; i < node->list.entry_count; i++)
|
||||
walk(walker, node->list.entries[i]);
|
||||
// NOTE: destroy block env here
|
||||
{
|
||||
fzl_walk_res_t res = walk(walker, node->list.entries[i]);
|
||||
if (res.error)
|
||||
return res;
|
||||
}
|
||||
push_op(walker, FZL_OPCODE_RM_ENVTABLE);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_CALL:
|
||||
if (node->list.entry_count > 0)
|
||||
if (node->call.args_count > 0)
|
||||
{
|
||||
for (size_t i = 0; i < node->list.entry_count; i++)
|
||||
for (size_t i = 0; i < node->call.args_count; i++)
|
||||
{
|
||||
walk(walker, node->list.entries[i]);
|
||||
push_op(walker, FZL_OPCODE_PUSHARG);
|
||||
fzl_walk_res_t res = walk(walker, node->call.args[i]);
|
||||
if (res.error)
|
||||
return res;
|
||||
}
|
||||
}
|
||||
// NOTE: you need to implement dynamic names call
|
||||
// NOTE: you need to implement dynamic names call too
|
||||
push_op(walker, FZL_OPCODE_CALL_BUILTIN);
|
||||
|
||||
if (strcmp(node->token->value, "printline") == 0)
|
||||
{
|
||||
push_word(walker, FZL_BUILTIN_PRINTLINE);
|
||||
if (node->call.args_count < 1)
|
||||
{
|
||||
snprintf(wlast_err, sizeof(wlast_err),
|
||||
"'printline' call with zero arguments at %ld:%ld",
|
||||
node->token->ln, node->token->cn);
|
||||
return (fzl_walk_res_t){.error = true, .error_msg = wlast_err};
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
snprintf(wlast_err, sizeof(wlast_err), "call to unknown function '%s'",
|
||||
(char *)node->token->value);
|
||||
return false;
|
||||
snprintf(wlast_err, sizeof(wlast_err),
|
||||
"call to unknown function '%s'", (char *)node->token->value);
|
||||
return (fzl_walk_res_t){.error = true, .error_msg = wlast_err};
|
||||
}
|
||||
current_res = (fzl_walk_res_t){.type = FZL_TYPE_NULL, .error = false};
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_LITERAL:
|
||||
if (node->token->type == FZL_TOKTYPE_STR_LITERAL)
|
||||
{
|
||||
size_t n = add_const(
|
||||
walker, (fzl_const_t){.type = FZL_CONSTTYPE_STRING,
|
||||
.as.string.chars = node->token->value,
|
||||
@@ -134,20 +237,196 @@ bool walk(fzl_walker_t *walker, fzl_node_t *node)
|
||||
|
||||
push_op(walker, FZL_OPCODE_LOADCONST);
|
||||
push_word(walker, n);
|
||||
current_res =
|
||||
(fzl_walk_res_t){.type = FZL_TYPE_STRING, .error = false};
|
||||
}
|
||||
else if (node->token->type == FZL_TOKTYPE_NUM_LITERAL)
|
||||
{
|
||||
push_op(walker, FZL_OPCODE_PUSHNUM);
|
||||
|
||||
uint64_t num = strtol(node->token->value, NULL, 10);
|
||||
push_word(walker, num);
|
||||
current_res = (fzl_walk_res_t){.type = FZL_TYPE_INT, .error = false};
|
||||
}
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_ADD:
|
||||
case FZL_NODETYPE_SUB:
|
||||
case FZL_NODETYPE_MUL:
|
||||
case FZL_NODETYPE_DIV:
|
||||
case FZL_NODETYPE_POW:
|
||||
case FZL_NODETYPE_MOD:
|
||||
{
|
||||
fzl_walk_res_t lres = walk(walker, node->binary_op.left);
|
||||
if (lres.error)
|
||||
return lres;
|
||||
|
||||
fzl_walk_res_t rres = walk(walker, node->binary_op.right);
|
||||
if (rres.error)
|
||||
return rres;
|
||||
|
||||
if (lres.type < FZL_TYPE_INT8)
|
||||
{
|
||||
fzl_token_t *ltok = node->binary_op.left->token;
|
||||
snprintf(wlast_err, sizeof(wlast_err),
|
||||
"left operand at %ld:%ld is not compatible to integer types",
|
||||
ltok->ln, ltok->cn);
|
||||
return (fzl_walk_res_t){.error = true, .error_msg = wlast_err};
|
||||
}
|
||||
|
||||
if (rres.type < FZL_TYPE_INT8)
|
||||
{
|
||||
fzl_token_t *rtok = node->binary_op.right->token;
|
||||
snprintf(
|
||||
wlast_err, sizeof(wlast_err),
|
||||
"right operand at %ld:%ld is not compatible to integer types",
|
||||
rtok->ln, rtok->cn);
|
||||
return (fzl_walk_res_t){.error = true, .error_msg = wlast_err};
|
||||
}
|
||||
|
||||
switch (node->type)
|
||||
{
|
||||
case FZL_NODETYPE_ADD:
|
||||
push_op(walker, FZL_OPCODE_ADD);
|
||||
break;
|
||||
case FZL_NODETYPE_SUB:
|
||||
push_op(walker, FZL_OPCODE_SUB);
|
||||
break;
|
||||
case FZL_NODETYPE_MUL:
|
||||
push_op(walker, FZL_OPCODE_MUL);
|
||||
break;
|
||||
case FZL_NODETYPE_DIV:
|
||||
push_op(walker, FZL_OPCODE_DIV);
|
||||
break;
|
||||
case FZL_NODETYPE_POW:
|
||||
push_op(walker, FZL_OPCODE_POW);
|
||||
break;
|
||||
case FZL_NODETYPE_MOD:
|
||||
push_op(walker, FZL_OPCODE_MOD);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
current_res = (fzl_walk_res_t){.type = lres.type, .error = false};
|
||||
break;
|
||||
}
|
||||
|
||||
case FZL_NODETYPE_UNARY_MINUS:
|
||||
{
|
||||
fzl_walk_res_t res = walk(walker, node->unary_op.operand);
|
||||
if (res.error)
|
||||
return res;
|
||||
|
||||
if (res.type < FZL_TYPE_INT8)
|
||||
{
|
||||
fzl_token_t *tok = node->unary_op.operand->token;
|
||||
snprintf(wlast_err, sizeof(wlast_err),
|
||||
"incompatible with unary operator type at %ld:%ld", tok->ln,
|
||||
tok->cn);
|
||||
return (fzl_walk_res_t){.error = true, .error_msg = wlast_err};
|
||||
}
|
||||
|
||||
push_op(walker, FZL_OPCODE_NEG);
|
||||
current_res = (fzl_walk_res_t){.type = res.type, .error = false};
|
||||
break;
|
||||
}
|
||||
|
||||
case FZL_NODETYPE_DECLARATION:
|
||||
{
|
||||
fzl_symtable_res_t res = fzl_symt_put(
|
||||
walker->syms[walker->sym_top - 1],
|
||||
node->var.identifier->token->value, tok_to_type(node->var.type));
|
||||
|
||||
fzl_token_t *id_tok = node->var.identifier->token;
|
||||
size_t const_n = add_const(
|
||||
walker, (fzl_const_t){.type = FZL_CONSTTYPE_STRING,
|
||||
.as.string.chars = id_tok->value,
|
||||
.as.string.length = id_tok->value_sz});
|
||||
|
||||
push_op(walker, FZL_OPCODE_LOADCONST);
|
||||
push_word(walker, const_n);
|
||||
|
||||
push_op(walker, FZL_OPCODE_STORE);
|
||||
push_op(walker, FZL_OPCODE_LOADCONST);
|
||||
push_word(walker, const_n);
|
||||
|
||||
current_res = (fzl_walk_res_t){.type = FZL_TYPE_NULL, .error = false};
|
||||
break;
|
||||
}
|
||||
|
||||
case FZL_NODETYPE_ASSIGN:
|
||||
{
|
||||
fzl_walk_res_t rres = walk(walker, node->binary_op.right);
|
||||
if (rres.error)
|
||||
return rres;
|
||||
|
||||
if (node->binary_op.left->type == FZL_NODETYPE_DECLARATION)
|
||||
{
|
||||
fzl_walk_res_t lres = walk(walker, node->binary_op.left);
|
||||
if (lres.error)
|
||||
return lres;
|
||||
}
|
||||
else if (node->binary_op.left->type == FZL_NODETYPE_IDENTIFIER)
|
||||
{
|
||||
fzl_sym_t sym;
|
||||
bool res = fzl_symt_get(walker->syms[walker->sym_top - 1],
|
||||
node->binary_op.left->token->value, &sym);
|
||||
if (!res) // NOTE: refactor this before implement blocks, fns, etc
|
||||
{
|
||||
fzl_token_t *tok = node->binary_op.left->token->value;
|
||||
snprintf(wlast_err, sizeof(wlast_err),
|
||||
"name '%s' is not exists at %ld:%ld", (char *)tok->value,
|
||||
tok->ln, tok->cn);
|
||||
return (fzl_walk_res_t){.error = true, .error_msg = wlast_err};
|
||||
}
|
||||
|
||||
if (!is_compatible(sym.type, rres.type))
|
||||
{
|
||||
fzl_token_t *tok = node->binary_op.right->token;
|
||||
snprintf(wlast_err, sizeof(wlast_err),
|
||||
"R-Value is not compatible with L-Value at %ld:%ld",
|
||||
tok->ln, tok->cn);
|
||||
return (fzl_walk_res_t){.error = true, .error_msg = wlast_err};
|
||||
}
|
||||
|
||||
size_t const_n = add_const(
|
||||
walker, (fzl_const_t){.type = FZL_CONSTTYPE_STRING,
|
||||
.as.string.chars = sym.name,
|
||||
.as.string.length =
|
||||
node->binary_op.left->token->value_sz});
|
||||
push_op(walker, FZL_OPCODE_LOADCONST);
|
||||
push_word(walker, const_n);
|
||||
}
|
||||
|
||||
push_op(walker, FZL_OPCODE_WRITE);
|
||||
current_res = (fzl_walk_res_t){.type = rres.type, .error = false};
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
node = node->next;
|
||||
}
|
||||
|
||||
return true;
|
||||
return current_res;
|
||||
}
|
||||
|
||||
// Walk entire AST recursevily
|
||||
int fzl_walk(fzl_walker_t *walker)
|
||||
{
|
||||
bool result = walk(walker, walker->root);
|
||||
return !result && wlast_err[0] ? -1 : 0;
|
||||
if (!walker)
|
||||
{
|
||||
snprintf(wlast_err, sizeof(wlast_err), "walker is null");
|
||||
return -1;
|
||||
}
|
||||
|
||||
push_op(walker, FZL_OPCODE_MK_ENVTABLE);
|
||||
fzl_walk_res_t result = walk(walker, walker->root);
|
||||
push_op(walker, FZL_OPCODE_RM_ENVTABLE);
|
||||
|
||||
if (result.error && !wlast_err[0])
|
||||
snprintf(wlast_err, sizeof(wlast_err), "%s", result.error_msg);
|
||||
|
||||
return result.error ? -1 : 0;
|
||||
}
|
||||
|
||||
// --- Walking zone end ---
|
||||
|
||||
+16
-5
@@ -102,7 +102,7 @@ int main(int argc, char **argv)
|
||||
if (args->verbose)
|
||||
printf("> analyze\n-> reading file...");
|
||||
|
||||
char source[8192];
|
||||
char *source;
|
||||
FILE *fd = fopen(args->filepath, "r");
|
||||
if (!fd)
|
||||
{
|
||||
@@ -114,11 +114,15 @@ int main(int argc, char **argv)
|
||||
size_t fsz = ftell(fd);
|
||||
rewind(fd);
|
||||
|
||||
source = malloc(fsz + 1);
|
||||
fread(source, fsz, 1, fd);
|
||||
fclose(fd);
|
||||
source[fsz] = '\0';
|
||||
|
||||
if (args->verbose)
|
||||
{
|
||||
printf("ok!\n");
|
||||
printf("--> Red %ld bytes\n", fsz);
|
||||
printf("-> tokenizing...");
|
||||
}
|
||||
|
||||
@@ -189,8 +193,14 @@ int main(int argc, char **argv)
|
||||
printf("---> %s\n", walker->consts->array[i].as.string.chars);
|
||||
|
||||
printf("--> Ops:\n");
|
||||
printf("---> ");
|
||||
for (size_t i = 0; i < walker->op_count; i++)
|
||||
printf("---> %lu\n", walker->ops[i]);
|
||||
{
|
||||
if (i > 0 && i < walker->op_count)
|
||||
printf("->");
|
||||
printf("%lu", walker->ops[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
printf("-> preparing final write\n");
|
||||
}
|
||||
@@ -199,11 +209,11 @@ int main(int argc, char **argv)
|
||||
if (args->outpath)
|
||||
strcpy(outpath, args->outpath);
|
||||
else
|
||||
strcpy(outpath, "a.fzlc");
|
||||
strcpy(outpath, "a.fzb");
|
||||
|
||||
if (args->verbose)
|
||||
{
|
||||
printf("-> output file path is: %s", outpath);
|
||||
printf("-> output file path is: %s\n", outpath);
|
||||
printf("-> writing file...");
|
||||
}
|
||||
|
||||
@@ -218,10 +228,11 @@ int main(int argc, char **argv)
|
||||
if (args->verbose)
|
||||
printf("ok!\n");
|
||||
|
||||
free(source);
|
||||
free(args);
|
||||
fzl_freereader(rd);
|
||||
fzl_freeparser(parser);
|
||||
fzl_freeast(parser->root);
|
||||
fzl_freeparser(parser);
|
||||
fzl_freewalker(walker);
|
||||
fzl_freewriter(writer);
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ static uint32_t fzl_hash_const(fzl_const_t c)
|
||||
switch (c.type)
|
||||
{
|
||||
case FZL_CONSTTYPE_STRING:
|
||||
data = (const uint8_t *)&c.as.string.chars;
|
||||
data = (const uint8_t *)c.as.string.chars;
|
||||
size = c.as.string.length;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
fzl_symtable_t *fzl_mksymt()
|
||||
{
|
||||
fzl_symtable_t *tbl = malloc(sizeof(fzl_symtable_t));
|
||||
if (!tbl)
|
||||
return NULL;
|
||||
|
||||
tbl->remainder = 2;
|
||||
tbl->size = 0;
|
||||
tbl->symbols = malloc(tbl->remainder * sizeof(fzl_sym_t));
|
||||
if (!tbl->symbols)
|
||||
return NULL;
|
||||
|
||||
return tbl;
|
||||
}
|
||||
|
||||
fzl_symtable_res_t fzl_symt_put(fzl_symtable_t *symt, char *name,
|
||||
fzl_type_t type)
|
||||
{
|
||||
if (!symt)
|
||||
return FZL_SYMTABLE_RESULT_ERR;
|
||||
|
||||
for (size_t i = 0; i < symt->size; i++)
|
||||
if (strcmp(symt->symbols[i].name, name) == 0)
|
||||
return FZL_SYMTABLE_RESULT_EXISTS;
|
||||
|
||||
if (symt->remainder <= symt->size)
|
||||
{
|
||||
symt->remainder *= 2;
|
||||
symt->symbols = realloc(symt->symbols, symt->remainder * sizeof(fzl_sym_t));
|
||||
}
|
||||
|
||||
symt->symbols[symt->size++] = (fzl_sym_t){.name = name, .type = type};
|
||||
return FZL_SYMTABLE_RESULT_OK;
|
||||
}
|
||||
|
||||
bool fzl_symt_get(fzl_symtable_t *symt, char *name, fzl_sym_t *res)
|
||||
{
|
||||
if (!symt)
|
||||
return false;
|
||||
|
||||
for (size_t i = 0; i < symt->size; i++)
|
||||
{
|
||||
if (strcmp(symt->symbols[i].name, name) == 0)
|
||||
{
|
||||
*res = symt->symbols[i];
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void fzl_freesymt(fzl_symtable_t *symt)
|
||||
{
|
||||
if (!symt)
|
||||
return;
|
||||
|
||||
if (symt->symbols)
|
||||
free(symt->symbols);
|
||||
|
||||
free(symt);
|
||||
}
|
||||
+220
-19
@@ -1,12 +1,17 @@
|
||||
#include "common/fzl_builtins.h"
|
||||
#include "common/fzl_opcodes.h"
|
||||
#include "compiler/utils/fzl_const_pool.h"
|
||||
#include "vm/fzl_vm_env.h"
|
||||
#include <common/fzl_builtins.h>
|
||||
#include <common/fzl_opcodes.h>
|
||||
#include <common/fzl_types.h>
|
||||
#include <common/fzl_value.h>
|
||||
#include <compiler/utils/fzl_const_pool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <vm/fzl_vm.h>
|
||||
|
||||
// NOTE: need to add optimized arithmetics in near future
|
||||
|
||||
char last_err[256];
|
||||
|
||||
static char *expect_hdr = "FZL\0\1";
|
||||
@@ -25,10 +30,13 @@ fzl_vm_t *fzl_mkvm(const char *filepath)
|
||||
vm->ops = NULL;
|
||||
vm->ops_count = 0;
|
||||
vm->ip = 0;
|
||||
memset(vm->stack, 0, sizeof(vm->stack));
|
||||
vm->stack_top = 0;
|
||||
memset(vm->arg_stack, 0, sizeof(vm->arg_stack));
|
||||
vm->arg_top = 0;
|
||||
vm->dtop = 0;
|
||||
vm->ctop = 0;
|
||||
vm->etop = 0;
|
||||
|
||||
memset(vm->dstack, 0, sizeof(vm->dstack));
|
||||
memset(vm->cstack, 0, sizeof(vm->cstack));
|
||||
memset(vm->estack, 0, sizeof(vm->estack));
|
||||
|
||||
FILE *fd = fopen(filepath, "rb");
|
||||
if (!fd)
|
||||
@@ -58,7 +66,7 @@ fzl_vm_t *fzl_mkvm(const char *filepath)
|
||||
if (consts_count > 0)
|
||||
{
|
||||
vm->consts_count = consts_count;
|
||||
vm->consts = malloc(consts_count * sizeof(fzl_const_t));
|
||||
vm->consts = malloc(consts_count * sizeof(fzl_value_t));
|
||||
|
||||
for (size_t i = 0; i < consts_count; i++)
|
||||
{
|
||||
@@ -111,16 +119,109 @@ int fzl_vm_run(fzl_vm_t *vm)
|
||||
vm->ip = 0;
|
||||
while (vm->ip < vm->ops_count)
|
||||
{
|
||||
uint64_t opcode = vm->ops[vm->ip];
|
||||
fzl_opcode_t opcode = vm->ops[vm->ip];
|
||||
|
||||
switch (opcode)
|
||||
{
|
||||
case FZL_OPCODE_STORE:
|
||||
if (vm->dtop < 1)
|
||||
{
|
||||
snprintf(last_err, sizeof(last_err),
|
||||
"runtime error - expected var name in data stack");
|
||||
return -1;
|
||||
}
|
||||
|
||||
fzl_value_t varname = vm->dstack[--vm->dtop];
|
||||
fzl_env_storevalue(vm->estack[vm->etop - 1], (char *)varname.as.s.value,
|
||||
(fzl_value_t){});
|
||||
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_WRITE:
|
||||
if (vm->dtop < 2)
|
||||
{
|
||||
snprintf(last_err, sizeof(last_err),
|
||||
"runtime error - expected 2 values in data stack");
|
||||
return -1;
|
||||
}
|
||||
|
||||
fzl_value_t writevar = vm->dstack[--vm->dtop];
|
||||
fzl_value_t value = vm->dstack[--vm->dtop];
|
||||
|
||||
fzl_vm_env_wresult_t wresult = fzl_env_writevalue(
|
||||
vm->estack[vm->etop - 1], (char *)writevar.as.s.value, value);
|
||||
if (wresult != FZL_VM_ENV_WRESULT_OK)
|
||||
{
|
||||
switch (wresult)
|
||||
{
|
||||
case FZL_VM_ENV_WRESULT_ERR:
|
||||
snprintf(last_err, sizeof(last_err),
|
||||
"runtime error - failed to write variable");
|
||||
return -1;
|
||||
|
||||
case FZL_VM_ENV_WRESULT_ERR_EXISTS:
|
||||
snprintf(last_err, sizeof(last_err),
|
||||
"runtime error - redefenition of '%s'",
|
||||
writevar.as.s.value);
|
||||
return -1;
|
||||
|
||||
case FZL_VM_ENV_WRESULT_ERR_NOT_FOUND:
|
||||
snprintf(last_err, sizeof(last_err),
|
||||
"runtime error - cannot find name '%s'",
|
||||
writevar.as.s.value);
|
||||
return -1;
|
||||
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
vm->dstack[vm->dtop++] = value;
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_MK_ENVTABLE:
|
||||
vm->estack[vm->etop++] = fzl_mkenv();
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_RM_ENVTABLE:
|
||||
fzl_freeenv(vm->estack[--vm->etop]);
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_LOOKUP_ENV:
|
||||
{
|
||||
if (vm->dtop < 1)
|
||||
{
|
||||
snprintf(last_err, sizeof(last_err),
|
||||
"runtime error - LOOKUP_ENV: empty data stack");
|
||||
return -1;
|
||||
}
|
||||
|
||||
fzl_value_t val = vm->dstack[--vm->dtop];
|
||||
fzl_value_t env_val =
|
||||
fzl_env_lookup(vm->estack[vm->etop - 1], (char *)val.as.s.value);
|
||||
vm->dstack[vm->dtop++] = env_val;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case FZL_OPCODE_CALL_BUILTIN:
|
||||
{
|
||||
uint64_t fn_id = vm->ops[++vm->ip];
|
||||
if (fn_id == FZL_BUILTIN_PRINTLINE)
|
||||
{
|
||||
fzl_const_t arg = vm->arg_stack[--vm->arg_top];
|
||||
printf("%s\n", arg.as.string.chars);
|
||||
fzl_value_t arg = vm->dstack[--vm->dtop];
|
||||
if (arg.type == FZL_TYPE_STRING)
|
||||
printf("%s\n", arg.as.s.value);
|
||||
else if (arg.type == FZL_TYPE_CHAR)
|
||||
printf("%c\n", arg.as.c);
|
||||
else if (arg.type == FZL_TYPE_INT)
|
||||
printf("%ld\n", arg.as.i);
|
||||
else if (arg.type == FZL_TYPE_UINT)
|
||||
printf("%lu\n", arg.as.u);
|
||||
else if (arg.type == FZL_TYPE_BOOL)
|
||||
printf("%b\n", arg.as.b);
|
||||
else if (arg.type == FZL_TYPE_NULL)
|
||||
printf("<null>\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -128,10 +229,11 @@ int fzl_vm_run(fzl_vm_t *vm)
|
||||
"runtime error - undefined builtin %lu", fn_id);
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_LOADCONST:
|
||||
{
|
||||
uint64_t cn = vm->ops[++vm->ip];
|
||||
if (vm->consts_count <= cn)
|
||||
{
|
||||
@@ -139,22 +241,121 @@ int fzl_vm_run(fzl_vm_t *vm)
|
||||
return -1;
|
||||
}
|
||||
fzl_const_t c = vm->consts[cn];
|
||||
vm->stack[vm->stack_top++] = c;
|
||||
vm->dstack[vm->dtop++] =
|
||||
(fzl_value_t){.type = FZL_TYPE_STRING,
|
||||
.as.s.length = c.as.string.length,
|
||||
.as.s.value = c.as.string.chars};
|
||||
}
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_PUSHARG:
|
||||
if (vm->stack_top < 1)
|
||||
case FZL_OPCODE_ADD:
|
||||
if (vm->dtop < 2)
|
||||
{
|
||||
snprintf(last_err, sizeof(last_err),
|
||||
"runtime error - cannot push argument: stack is empty");
|
||||
"runtime error - expected 2 values in stack");
|
||||
return -1;
|
||||
}
|
||||
vm->arg_stack[vm->arg_top++] = vm->stack[--vm->stack_top];
|
||||
fzl_value_t add1 = vm->dstack[--vm->dtop];
|
||||
fzl_value_t add2 = vm->dstack[--vm->dtop];
|
||||
vm->dstack[vm->dtop++] =
|
||||
(fzl_value_t){.type = add1.type, .as.u = add1.as.u + add2.as.u};
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_SUB:
|
||||
if (vm->dtop < 2)
|
||||
{
|
||||
snprintf(last_err, sizeof(last_err),
|
||||
"runtime error - expected 2 values in stack");
|
||||
return -1;
|
||||
}
|
||||
fzl_value_t sub1 = vm->dstack[--vm->dtop];
|
||||
fzl_value_t sub2 = vm->dstack[--vm->dtop];
|
||||
vm->dstack[vm->dtop++] =
|
||||
(fzl_value_t){.type = add1.type, .as.u = sub1.as.u - sub2.as.u};
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_MUL:
|
||||
if (vm->dtop < 2)
|
||||
{
|
||||
snprintf(last_err, sizeof(last_err),
|
||||
"runtime error - expected 2 values in stack");
|
||||
return -1;
|
||||
}
|
||||
fzl_value_t mul1 = vm->dstack[--vm->dtop];
|
||||
fzl_value_t mul2 = vm->dstack[--vm->dtop];
|
||||
vm->dstack[vm->dtop++] =
|
||||
(fzl_value_t){.type = mul1.type, .as.u = mul1.as.u * mul2.as.u};
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_DIV:
|
||||
if (vm->dtop < 2)
|
||||
{
|
||||
snprintf(last_err, sizeof(last_err),
|
||||
"runtime error - expected 2 values in stack");
|
||||
return -1;
|
||||
}
|
||||
fzl_value_t div1 = vm->dstack[--vm->dtop];
|
||||
fzl_value_t div2 = vm->dstack[--vm->dtop];
|
||||
vm->dstack[vm->dtop++] =
|
||||
(fzl_value_t){.type = div1.type, .as.u = div2.as.u / div1.as.u};
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_POW:
|
||||
if (vm->dtop < 2)
|
||||
{
|
||||
snprintf(last_err, sizeof(last_err),
|
||||
"runtime error - expected 2 values in stack");
|
||||
return -1;
|
||||
}
|
||||
fzl_value_t pow1 = vm->dstack[--vm->dtop];
|
||||
fzl_value_t pow2 = vm->dstack[--vm->dtop];
|
||||
|
||||
uint64_t pow_n = pow1.as.u;
|
||||
uint64_t pow_res = pow_n;
|
||||
uint64_t pow_times = pow2.as.u;
|
||||
for (size_t i = 1; i < pow_times; i++)
|
||||
pow_res *= pow_n;
|
||||
|
||||
vm->dstack[vm->dtop++] =
|
||||
(fzl_value_t){.type = pow1.type, .as.u = pow_res};
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_MOD:
|
||||
if (vm->dtop < 2)
|
||||
{
|
||||
snprintf(last_err, sizeof(last_err),
|
||||
"runtime error - expected 2 values in stack");
|
||||
return -1;
|
||||
}
|
||||
fzl_value_t mod1 = vm->dstack[--vm->dtop];
|
||||
fzl_value_t mod2 = vm->dstack[--vm->dtop];
|
||||
vm->dstack[vm->dtop++] =
|
||||
(fzl_value_t){.type = mod1.type, .as.u = mod2.as.u % mod1.as.u};
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_NEG:
|
||||
if (vm->dtop < 1)
|
||||
{
|
||||
snprintf(last_err, sizeof(last_err),
|
||||
"runtime error - expected value in stack");
|
||||
return -1;
|
||||
}
|
||||
fzl_value_t neg_n = vm->dstack[--vm->dtop];
|
||||
vm->dstack[vm->dtop++] =
|
||||
(fzl_value_t){.type = neg_n.type, .as.u = -neg_n.as.u};
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_PUSHNUM:
|
||||
{
|
||||
uint64_t push_n = vm->ops[++vm->ip];
|
||||
vm->dstack[vm->dtop++] =
|
||||
(fzl_value_t){.type = FZL_TYPE_UINT, .as.u = push_n};
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
snprintf(last_err, sizeof(last_err), "runtime error - unknown opcode %lu",
|
||||
opcode);
|
||||
snprintf(last_err, sizeof(last_err),
|
||||
"runtime error - unknown opcode %u", opcode);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
#include "common/fzl_opcodes.h"
|
||||
#include <stdio.h>
|
||||
#include <vm/fzl_vm.h>
|
||||
|
||||
void fzl_vm_print_ops(fzl_vm_t *vm)
|
||||
{
|
||||
if (!vm)
|
||||
return;
|
||||
|
||||
if (vm->ops_count < 1)
|
||||
{
|
||||
printf("no opcodes\n");
|
||||
return;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < vm->ops_count; i++)
|
||||
{
|
||||
switch (vm->ops[i])
|
||||
{
|
||||
case FZL_OPCODE_LOADCONST:
|
||||
printf("LOADCONST #%lu\n", vm->ops[++i]);
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_CALL_BUILTIN:
|
||||
printf("CALL_BUILTIN #%lu\n", vm->ops[++i]);
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_PUSHNUM:
|
||||
printf("PUSHNUM %lu\n", vm->ops[++i]);
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_MK_ENVTABLE:
|
||||
printf("MK_ENVTABLE\n");
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_RM_ENVTABLE:
|
||||
printf("RM_ENVTABLE\n");
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_STORE:
|
||||
printf("STORE\n");
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_LOOKUP_ENV:
|
||||
printf("LOOKUP_ENV\n");
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_WRITE:
|
||||
printf("WRITE\n");
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_ADD:
|
||||
case FZL_OPCODE_SUB:
|
||||
case FZL_OPCODE_MUL:
|
||||
case FZL_OPCODE_DIV:
|
||||
case FZL_OPCODE_POW:
|
||||
case FZL_OPCODE_MOD:
|
||||
printf("Math opcode\n");
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_NEG:
|
||||
printf("NEG\n");
|
||||
break;
|
||||
|
||||
default:
|
||||
printf("Unknown");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <common/fzl_value.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <vm/fzl_vm_env.h>
|
||||
|
||||
static const fzl_value_t null = {.type = FZL_TYPE_NULL};
|
||||
|
||||
fzl_vm_env_t *fzl_mkenv()
|
||||
{
|
||||
fzl_vm_env_t *env = malloc(sizeof(fzl_vm_env_t));
|
||||
env->remainder = 2;
|
||||
env->size = 0;
|
||||
env->table = malloc(env->remainder * sizeof(fzl_vm_env_entry_t));
|
||||
|
||||
return env;
|
||||
}
|
||||
|
||||
// Resize table if size eq to remainder
|
||||
static void resize_env_if_needed(fzl_vm_env_t *env)
|
||||
{
|
||||
if (env->remainder > env->size)
|
||||
return;
|
||||
|
||||
env->remainder *= 2;
|
||||
env->table = realloc(env->table, env->remainder * sizeof(fzl_vm_env_entry_t));
|
||||
}
|
||||
|
||||
fzl_value_t fzl_env_lookup(fzl_vm_env_t *env, char *name)
|
||||
{
|
||||
if (!env)
|
||||
return null;
|
||||
|
||||
for (size_t i = 0; i < env->size; i++)
|
||||
if (strcmp(env->table[i].name, name) == 0)
|
||||
return env->table[i].value;
|
||||
|
||||
return (fzl_value_t){.type = FZL_TYPE_NULL};
|
||||
}
|
||||
|
||||
bool fzl_env_contains(fzl_vm_env_t *env, char *name)
|
||||
{
|
||||
if (!env)
|
||||
return false;
|
||||
|
||||
for (size_t i = 0; i < env->size; i++)
|
||||
if (strcmp(env->table[i].name, name) == 0)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
fzl_vm_env_wresult_t fzl_env_storevalue(fzl_vm_env_t *env, char *name,
|
||||
fzl_value_t val)
|
||||
{
|
||||
if (!env)
|
||||
return FZL_VM_ENV_WRESULT_ERR;
|
||||
|
||||
resize_env_if_needed(env);
|
||||
if (fzl_env_contains(env, name))
|
||||
return FZL_VM_ENV_WRESULT_ERR_EXISTS;
|
||||
|
||||
env->table[env->size++] = (fzl_vm_env_entry_t){.name = name, .value = val};
|
||||
|
||||
return FZL_VM_ENV_WRESULT_OK;
|
||||
}
|
||||
|
||||
fzl_vm_env_wresult_t fzl_env_writevalue(fzl_vm_env_t *env, char *name,
|
||||
fzl_value_t newval)
|
||||
{
|
||||
if (!env)
|
||||
return FZL_VM_ENV_WRESULT_ERR;
|
||||
|
||||
for (size_t i = 0; i < env->size; i++)
|
||||
{
|
||||
if (strcmp(env->table[i].name, name) == 0)
|
||||
{
|
||||
env->table[i].value = newval;
|
||||
return FZL_VM_ENV_WRESULT_OK;
|
||||
}
|
||||
}
|
||||
|
||||
return FZL_VM_ENV_WRESULT_ERR_NOT_FOUND;
|
||||
}
|
||||
|
||||
void fzl_freeenv(fzl_vm_env_t *env)
|
||||
{
|
||||
if (!env)
|
||||
return;
|
||||
|
||||
if (env->table)
|
||||
free(env->table);
|
||||
|
||||
free(env);
|
||||
}
|
||||
@@ -9,6 +9,7 @@ typedef struct
|
||||
{
|
||||
char *filepath;
|
||||
bool help;
|
||||
bool debug;
|
||||
} run_args_t;
|
||||
|
||||
void print_hint()
|
||||
@@ -30,6 +31,7 @@ run_args_t *parse_args(int argc, char **argv)
|
||||
run_args_t *args = malloc(sizeof(run_args_t));
|
||||
args->filepath = NULL;
|
||||
args->help = false;
|
||||
args->debug = false;
|
||||
|
||||
for (size_t i = 0; i < argc; i++)
|
||||
{
|
||||
@@ -41,6 +43,8 @@ run_args_t *parse_args(int argc, char **argv)
|
||||
args->help = true;
|
||||
return args;
|
||||
}
|
||||
else if (strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--debug") == 0)
|
||||
args->debug = true;
|
||||
else
|
||||
{
|
||||
printf("Error: unknown flag '%s'.\n", argv[i]);
|
||||
@@ -79,12 +83,17 @@ int main(int argc, char **argv)
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (args->debug)
|
||||
fzl_vm_print_ops(vm);
|
||||
else
|
||||
{
|
||||
int result = fzl_vm_run(vm);
|
||||
if (result != 0)
|
||||
{
|
||||
printf("Error: %s\n", fzl_vm_geterror());
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
free(args);
|
||||
fzl_freevm(vm);
|
||||
|
||||
Reference in New Issue
Block a user