mirror of
https://github.com/ProgramSnail/Lama.git
synced 2025-12-06 06:48:48 +00:00
byterun build with analyzer
This commit is contained in:
parent
2cd7afe0c5
commit
4c34a63bb7
14 changed files with 808 additions and 32 deletions
7
byterun/include/analyzer.hpp
Normal file
7
byterun/include/analyzer.hpp
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
extern "C" {
|
||||
#include "utils.h"
|
||||
}
|
||||
|
||||
void analyze(const Bytefile &bf);
|
||||
|
|
@ -2,4 +2,4 @@
|
|||
|
||||
#include "parser.h"
|
||||
|
||||
void run(bytefile *bf, int argc, char **argv);
|
||||
void run(Bytefile *bf, int argc, char **argv);
|
||||
|
|
|
|||
|
|
@ -21,6 +21,6 @@
|
|||
|
||||
const char *read_cmd(char *ip);
|
||||
|
||||
bytefile *read_file(char *fname);
|
||||
Bytefile *read_file(char *fname);
|
||||
|
||||
// void dump_file(FILE *f, bytefile *bf);
|
||||
|
|
|
|||
71
byterun/include/parser.hpp
Normal file
71
byterun/include/parser.hpp
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
#pragma once
|
||||
|
||||
#include <ostream>
|
||||
|
||||
extern "C" {
|
||||
#include "utils.h"
|
||||
}
|
||||
|
||||
enum class Cmd : int8_t {
|
||||
BINOP,
|
||||
CONST,
|
||||
STRING,
|
||||
SEXP,
|
||||
STI,
|
||||
STA,
|
||||
JMP,
|
||||
END,
|
||||
RET,
|
||||
DROP,
|
||||
DUP,
|
||||
SWAP,
|
||||
ELEM,
|
||||
LD,
|
||||
LDA,
|
||||
ST,
|
||||
CJMPz,
|
||||
CJMPnz,
|
||||
BEGIN,
|
||||
CBEGIN,
|
||||
CLOSURE,
|
||||
CALLC,
|
||||
CALL,
|
||||
TAG,
|
||||
ARRAY,
|
||||
FAIL,
|
||||
LINE,
|
||||
PATT,
|
||||
Lread,
|
||||
Lwrite,
|
||||
Llength,
|
||||
Lstring,
|
||||
Barray,
|
||||
EXIT,
|
||||
_UNDEF_,
|
||||
};
|
||||
|
||||
Bytefile *read_file(const char *fname);
|
||||
|
||||
static inline int ip_read_int(char **ip, const Bytefile &bf) {
|
||||
if (*ip + sizeof(int) > bf.code_ptr + bf.code_size) {
|
||||
failure("last command is invalid, int parameter can not be read\n");
|
||||
}
|
||||
*ip += sizeof(int);
|
||||
return *(int *)((*ip) - sizeof(int));
|
||||
}
|
||||
|
||||
static inline uint8_t ip_read_byte(char **ip, const Bytefile &bf) {
|
||||
if (*ip + sizeof(char) > bf.code_ptr + bf.code_size) {
|
||||
failure("last command is invalid, byte parameter can not be read\n");
|
||||
}
|
||||
return *(*ip)++;
|
||||
}
|
||||
|
||||
static inline uint8_t ip_read_byte_unsafe(char **ip) { return *(*ip)++; }
|
||||
|
||||
static inline const char *ip_read_string(char **ip, const Bytefile &bf) {
|
||||
return get_string(&bf, ip_read_int(ip, bf));
|
||||
}
|
||||
|
||||
Cmd parse_command(char **ip, const Bytefile &bf);
|
||||
Cmd parse_command(char **ip, const Bytefile &bf, std::ostream &out);
|
||||
|
|
@ -55,7 +55,7 @@ struct State {
|
|||
void **stack;
|
||||
// void **sp; // stack pointer
|
||||
struct Frame *fp; // function frame pointer
|
||||
bytefile *bf;
|
||||
Bytefile *bf;
|
||||
int current_line;
|
||||
|
||||
bool is_closure_call;
|
||||
|
|
@ -65,7 +65,7 @@ struct State {
|
|||
char *call_ip; // prev instruction pointer (to remember jmp locations)
|
||||
};
|
||||
|
||||
void construct_state(bytefile *bf, struct State *s, void **stack);
|
||||
void construct_state(Bytefile *bf, struct State *s, void **stack);
|
||||
void cleanup_state(struct State *state);
|
||||
|
||||
static inline void s_failure(struct State *s, const char *msg) {
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ typedef struct {
|
|||
uint global_area_size; /* The size (in words) of global area */
|
||||
uint public_symbols_number; /* The number of public symbols */
|
||||
char buffer[0];
|
||||
} bytefile;
|
||||
} Bytefile;
|
||||
|
||||
static inline void exec_failure(const char *cmd, int line, aint offset,
|
||||
const char *msg) {
|
||||
|
|
@ -27,7 +27,7 @@ static inline void exec_failure(const char *cmd, int line, aint offset,
|
|||
}
|
||||
|
||||
/* Gets a string from a string table by an index */
|
||||
static inline const char *get_string(const bytefile *f, size_t pos) {
|
||||
static inline const char *get_string(const Bytefile *f, size_t pos) {
|
||||
if (pos >= f->stringtab_size) {
|
||||
failure("strinpg pos is out of range: %zu >= %i\n", pos, f->stringtab_size);
|
||||
}
|
||||
|
|
@ -35,18 +35,18 @@ static inline const char *get_string(const bytefile *f, size_t pos) {
|
|||
}
|
||||
|
||||
/* Gets a name for a public symbol */
|
||||
static inline const char *get_public_name(const bytefile *f, size_t i) {
|
||||
static inline const char *get_public_name(const Bytefile *f, size_t i) {
|
||||
if (i >= f->public_symbols_number) {
|
||||
failure("public number is out of range: %zu >= %i\n", i * 2,
|
||||
failure("public number is out of range: %zu >= %i\n", i,
|
||||
f->public_symbols_number);
|
||||
}
|
||||
return get_string(f, f->public_ptr[i * 2]);
|
||||
}
|
||||
|
||||
/* Gets an offset for a publie symbol */
|
||||
static inline size_t get_public_offset(const bytefile *f, size_t i) {
|
||||
if (i + 1 >= f->public_symbols_number) {
|
||||
failure("public number is out of range: %zu >= %i\n", i * 2 + 1,
|
||||
static inline size_t get_public_offset(const Bytefile *f, size_t i) {
|
||||
if (i >= f->public_symbols_number) {
|
||||
failure("public number is out of range: %zu >= %i\n", i,
|
||||
f->public_symbols_number);
|
||||
}
|
||||
return f->public_ptr[i * 2 + 1];
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue