diff options
author | chai <chaifix@163.com> | 2020-10-17 15:22:09 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2020-10-17 15:22:09 +0800 |
commit | 08ab881a480a73d6be005c4b4e1cf35814b53e3c (patch) | |
tree | 00297c01905cff519509598c5340a82c0fc2e941 /src |
Diffstat (limited to 'src')
45 files changed, 569 insertions, 0 deletions
diff --git a/src/nb.c b/src/nb.c new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/nb.c diff --git a/src/nb_common.h b/src/nb_common.h new file mode 100644 index 0000000..220d399 --- /dev/null +++ b/src/nb_common.h @@ -0,0 +1,18 @@ +#ifndef NB_COMMON_H +#define NB_COMMON_H + +// nbM(emory) memory +// nbX(lexer) lexer +// nbP(arser) parser +// nbV(m) vm +// nbD(ebug) debug +// nbL(load) module +// nbS(tring) string +// nb external API +// nb(map) map +// nb(list) list +// nb(hashset) hashset + +#define NB_API __declspec(dllexport) + +#endif
\ No newline at end of file diff --git a/src/nb_config.h b/src/nb_config.h new file mode 100644 index 0000000..07c74a3 --- /dev/null +++ b/src/nb_config.h @@ -0,0 +1,7 @@ +#ifndef NB_CONFIG_H +#define NB_CONFIG_H + +// 二进制文件开头标识 +#define NOOB_SIGNATURE "\033NB!" // <Esc>NB! + +#endif
\ No newline at end of file diff --git a/src/nb_debug.c b/src/nb_debug.c new file mode 100644 index 0000000..71fa67d --- /dev/null +++ b/src/nb_debug.c @@ -0,0 +1,10 @@ +#include "nb_debug.h" + +void nb_dump_value() { + +} + +void nb_dump_code() { + +} + diff --git a/src/nb_debug.h b/src/nb_debug.h new file mode 100644 index 0000000..6557393 --- /dev/null +++ b/src/nb_debug.h @@ -0,0 +1,8 @@ +#ifndef NB_DEBUG_H +#define NB_DEBUG_H + +void nb_dump_value(); + +void nb_dump_code(); + +#endif
\ No newline at end of file diff --git a/src/nb_dump.c b/src/nb_dump.c new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/nb_dump.c diff --git a/src/nb_dump.h b/src/nb_dump.h new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/nb_dump.h diff --git a/src/nb_gc.c b/src/nb_gc.c new file mode 100644 index 0000000..e26bdb9 --- /dev/null +++ b/src/nb_gc.c @@ -0,0 +1,4 @@ +#include "nb_gc.h" + + + diff --git a/src/nb_gc.h b/src/nb_gc.h new file mode 100644 index 0000000..79fed1a --- /dev/null +++ b/src/nb_gc.h @@ -0,0 +1,6 @@ +#ifndef NB_GC_H +#define NB_GC_H + + + +#endif
\ No newline at end of file diff --git a/src/nb_hashset.c b/src/nb_hashset.c new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/nb_hashset.c diff --git a/src/nb_lexer.c b/src/nb_lexer.c new file mode 100644 index 0000000..6ed985d --- /dev/null +++ b/src/nb_lexer.c @@ -0,0 +1,9 @@ +#include "nb_lexer.h" + +void nbI_lexing(NbVM* vm) { + +} + +static void next() { + +} diff --git a/src/nb_lexer.h b/src/nb_lexer.h new file mode 100644 index 0000000..8360c24 --- /dev/null +++ b/src/nb_lexer.h @@ -0,0 +1,8 @@ +#ifndef NB_LEXER_H +#define NB_LEXER_H + +#include "nb_vm.h" + +void nbI_lexing(NbVM* vm); + +#endif
\ No newline at end of file diff --git a/src/nb_libio.c b/src/nb_libio.c new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/nb_libio.c diff --git a/src/nb_libio.h b/src/nb_libio.h new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/nb_libio.h diff --git a/src/nb_libmath.c b/src/nb_libmath.c new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/nb_libmath.c diff --git a/src/nb_libmath.h b/src/nb_libmath.h new file mode 100644 index 0000000..5e723c3 --- /dev/null +++ b/src/nb_libmath.h @@ -0,0 +1,6 @@ +#ifndef NB_LIBMATH_H +#define NB_LIBMATH_H + + + +#endif
\ No newline at end of file diff --git a/src/nb_libos.c b/src/nb_libos.c new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/nb_libos.c diff --git a/src/nb_libos.h b/src/nb_libos.h new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/nb_libos.h diff --git a/src/nb_list.c b/src/nb_list.c new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/nb_list.c diff --git a/src/nb_map.c b/src/nb_map.c new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/nb_map.c diff --git a/src/nb_mem.c b/src/nb_mem.c new file mode 100644 index 0000000..a04e093 --- /dev/null +++ b/src/nb_mem.c @@ -0,0 +1,9 @@ +#include "nb_mem.h" + +void* nb_alloc(int size) { + +} + +void nb_free(void* mem) { + +} diff --git a/src/nb_mem.h b/src/nb_mem.h new file mode 100644 index 0000000..47567cb --- /dev/null +++ b/src/nb_mem.h @@ -0,0 +1,7 @@ +#ifndef NB_MEM_H +#define NB_MEM_H + +void* nb_alloc(int size); +void nb_free(void* mem); + +#endif
\ No newline at end of file diff --git a/src/nb_module.c b/src/nb_module.c new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/nb_module.c diff --git a/src/nb_module.h b/src/nb_module.h new file mode 100644 index 0000000..d59fd1d --- /dev/null +++ b/src/nb_module.h @@ -0,0 +1,6 @@ +#ifndef NB_MODULE_H +#define NB_MODULE_H + +void nb_load_module(const char* module_name); + +#endif
\ No newline at end of file diff --git a/src/nb_obj.c b/src/nb_obj.c new file mode 100644 index 0000000..16b7ef3 --- /dev/null +++ b/src/nb_obj.c @@ -0,0 +1,4 @@ +#include "nb_obj.h" + + + diff --git a/src/nb_obj.h b/src/nb_obj.h new file mode 100644 index 0000000..5793045 --- /dev/null +++ b/src/nb_obj.h @@ -0,0 +1,40 @@ +#ifndef NB_OBJ_H +#define NB_OBJ_H + +#define _GCHeader GCObject* next; int tt; int marked + +typedef struct { + _GCHeader; +} GCObject; + +typedef struct { + int n; +} Instruction; + + +typedef struct { + int p; + +} Proto; // 函数原型 + + +typedef struct { + Proto* proto; + +} Closure; // 闭包 + + +typedef struct { + _GCHeader; + +} Map; + +typedef struct { + _GCHeader; +} Hashset; + +typedef struct { + _GCHeader; +} List; + +#endif
\ No newline at end of file diff --git a/src/nb_opcode.h b/src/nb_opcode.h new file mode 100644 index 0000000..7bd9710 --- /dev/null +++ b/src/nb_opcode.h @@ -0,0 +1,224 @@ +#ifndef NB_OPCODE_H +#define NB_OPCODE_H + +typedef enum { + // import a external module + OP_IMPORT, // IMPORT + + // load class, enum, or mask from a module + OP_LOAD_EXTERNAL, // LOAD_EXTERNAL nameOfModule | + + OP_PUSH_CONSTANT, // PUSH_CONSTANT i | 1 + OP_PUSH_NULL, // PUSH_NULL | 1 + OP_PUSH_TRUE, // PUSH_TRUE | 1 + OP_PUSH_FALSE, // PUSH_FALSE | 1 + + // local-scoped variables + OP_LOAD_LOCAL, // LOAD_LOCAL i | 1 + OP_STORE_LOCAL, // STORE_LOCAL i | -1 + + // load or store upvalue + OP_LOAD_UPVALUE, // LOAD_UPVALUE i | 1 + OP_STORE_UPVALUE, // STORE_UPVALUE i | -1 + + // file-scoped variables + OP_LOAD_MODULE, // LOAD_MODULE i | 1 + OP_STORE_MODULE, // STORE_MODULE i | -1 + + OP_POP, // POP | 1 + OP_POPN, // POPN numOfValue | n + + OP_JUMP, // JUMP i | 0 + OP_JUMP_IF_FALSE, // JUMP_IF_FALSE | + OP_RETURN, // RETURN numOfReturn | -numOfReturn + OP_RETURN_NULL, // RETURN_NULL | + + // instansiate class + OP_INSTANTIATE, // INSTANTIATE numOfArgs | + + // call methods or functions + OP_CALL, // CALL numOfArgs | -(1+numOfArgs) + OP_OPERATOR, // OPERATOR operatorId | -(1+numOfArgs) + + // define class and method + OP_CLASS, // CLASS | 0 + OP_METHOD, // METHOD + OP_METHOD_STATIC, // METHOD_STATIC + + // load and store member of object or static member of class + OP_LOAD_FIELD, // LOAD_FIELD + OP_STORE_FIELD, // STORE_FIELD + + // call base class method, handle base. in methods + OP_CALL_BASE, // CALL_BASE numOfArgs | + + OP_TYPEOF, // + OP_INSTANCEOF, // + + // define enum or mask + OP_ENUM, // ENUM numOfElements | -(numOfElements) + OP_MASK, // MASK numOfElements | -(numOfElements) + + // create map, list or hashset + OP_MAP, + OP_LIST, + OP_HASHSET, + +} OpCode; + +// 对于运算符,使用 CALL 指令,运算符的操作数个数是确定的 +// operator numOfArgs pop +// + 2 +// ++ 1 +// += 2 +// - 2 +// -- 1 +// -= 2 +// * 2 +// *= 2 +// / 2 +// /= 2 +// ^ 2 +// ^= 2 +// % 2 +// %= 2 +// >> 2 +// >>= 2 +// << 2 +// <<= 2 +// & 2 +// | 2 +// ! 1 +// +// && 2 +// &= 2 +// || 2 +// |= 2 +// == 2 +// > 2 +// >= 2 +// < 2 +// <= 2 +// != 2 +// + + +// 导入模块流程 +// import Test; +// IMPORT +// +// import Test for CFoo; +// IMPORT "Test" +// STORE_MODULE +// POP +// +// import Test for CFoo as CClass; +// IMPORT "Test" +// LOAD_EXTERNAL "CFoo" +// STORE_MODULE 0 // "CClass" +// POPN 2 + + +// 方法调用 +// var foo = new CClass(); +// var b = 20; +// foo.Func(10, b); +// LOAD_LOCAL 1 // foo +// LOAD_FIELD "Func" +// LOAD_LOCAL 1 // foo +// PUSH_CONSTANT 10 +// LOAD_LOCAL 2 // b +// CALL 3 +// +// var c = a + b; +// LOAD_LOCAL 1 // a +// LOAD_LOCAL 2 // b +// OPERATOR 1 // + + + +// 函数调用 +// foo = func(10); +// LOAD_MODULE 1 // func +// PUSH_CONSTANT 1 // 10 +// CALL 1 +// STORE_LOCAL 2 // foo + +// 对象方法和函数\类方法的不同在于对象方法会把self放在某个upvalue +// 处理self.调用 +// class CFoo { +// function foo(a) { print(self.b);} +// static function foo2(a) { print(a);} +// } +// function foo3(a) {print(a);} +// foo和另两个的差别在于后两个索引到的self upvalue是空的,而foo的 +// 能够索引到这个upvalue, +// + +// 实例化 +// foo = new CClass(10, 30); +// LOAD_MODULE 1 // CClass +// PUSH_CONSTANT 4 // 10 +// PUSH_CONSTANT 5 // 30 +// INSTANTIATE 2 +// STORE_LOCAL 2 // foo + + +// 创建enum\mask +// enum EColor { White = 1, Blue = 2, Yellow = 3 } +// CONSTANT "EColor" +// ENUM // EColor +// CONSTANT 1 +// STORE_FIELD "White" // White = 1 +// CONSTANT 2 +// STORE_FIELD "Blue" // Blue = 1 +// CONSTANT 3 +// STORE_FIELD "Yellow" // Yellow = 1 +// POP // EColor + + +// 创建map +// var foo = {"keyA" : 10, "keyB" : "hello"}; +// foo["keyC"] = 20; +// MAP +// CONSTANT 1 // 10 +// STORE_FIELD "keyA" // "keyA" : 10 +// CONSTANT "hello" +// STORE_FIELD "keyB" // "keyB" : "hello" +// LOAD_LOCAL 1 // foo +// CONSTATN 20 +// STORE_FIELD "keyC" + + +// 创建list\hashset +// var foo = [1,2,3]; +// LIST +// CONSTANT 1 // 1 +// STORE_FIELD +// CONSTANT 2 +// STORE_FIELD +// CONSTANT 3 +// STORE_FIELD + + +// 构建闭包,实现观察者模式的方法 +// class CFoo { +// function OnEnable() { +// var this = self; +// // self 这里会作为upvalue +// onClick += function(...){this.OnClickCallback(...);}; +// } +// } + +// self.用来索引self upvalue,如果是方法,就会索引对象 +// base.用来索引base upvalue +// type.用来索引type upvalue +// basetype.用来索引基类的upvalue + +// .运算符只能运用于对象或者map\list\hashset,会设置的upvalue: +// * self + +// 类不能用::或者.索引对象方法,只能索引到static成员 + +// 在init外部不能创建新的对象成员 + +#endif
\ No newline at end of file diff --git a/src/nb_parser.c b/src/nb_parser.c new file mode 100644 index 0000000..6b2bfeb --- /dev/null +++ b/src/nb_parser.c @@ -0,0 +1,3 @@ +#include "nb_parser.h" + + diff --git a/src/nb_parser.h b/src/nb_parser.h new file mode 100644 index 0000000..d84bf8d --- /dev/null +++ b/src/nb_parser.h @@ -0,0 +1,6 @@ +#ifndef NB_PARSER_H +#define NB_PARSER_H + + + +#endif
\ No newline at end of file diff --git a/src/nb_preproc.c b/src/nb_preproc.c new file mode 100644 index 0000000..1f2e380 --- /dev/null +++ b/src/nb_preproc.c @@ -0,0 +1,9 @@ +#include "nb_preproc.h" + +void nb_set_macros(const char* macros[], int count) { + +} + +void nb_preprocess() { + +}
\ No newline at end of file diff --git a/src/nb_preproc.h b/src/nb_preproc.h new file mode 100644 index 0000000..24aa12a --- /dev/null +++ b/src/nb_preproc.h @@ -0,0 +1,16 @@ +#ifndef NB_PREPROC_H +#define NB_PREPROC_H + +#include "nb_vm.h" + +/* +* 预处理 +* #define +* #undef +* #if +*/ +void nb_set_macros(NbVM* vm, const char* macros[], int count); + +void nb_preprocess(NbVM* vm); + +#endif
\ No newline at end of file diff --git a/src/nb_string.c b/src/nb_string.c new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/nb_string.c diff --git a/src/nb_string.h b/src/nb_string.h new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/nb_string.h diff --git a/src/nb_token.h b/src/nb_token.h new file mode 100644 index 0000000..6f92dcb --- /dev/null +++ b/src/nb_token.h @@ -0,0 +1,104 @@ +#ifndef NB_TOKEN_H +#define NB_TOKEN_H + +#define TOKEN_INDEX_START 32 + +typedef enum { + TK_NULL = TOKEN_INDEX_START, // null + + TK_PLUS, // + + TK_PLUSPLUS, // ++ + TK_PLUSEQ, // += + TK_MINUS, // - + TK_MINUSMINUS, // -- + TK_MINUSEQ, // -= + TK_MULTIPLY, // * + TK_MULTIEQ, // *= + TK_SLASH, // / + TK_SLASHEQ, // /= + TK_CARET, // ^ + TK_CARETEQ, // ^= + TK_PERSENT, // % + TK_PERSENTEQ, // %= + TK_BANG, // ! + //TK_AT, // @ + //TK_QUOT, // " + TK_COMMA, // , + TK_DOT, // . + TK_DOTS, // ... + TK_COLON, // : + TK_SEMICOLON, // ; + TK_QUESTION, // ? + TK_RSHIFT, // >> + TK_RSHIFTEQ, // >>= + TK_LSHIFT, // << + TK_LSHIFTEQ, // <<= + + TK_AMP, // & + TK_AMPAMP, // && + TK_AMPEQ, // &= + TK_PIPE, // | + TK_PIPEPIPE, // || + TK_PIPEEQ, // |= + + TK_EQ, // = + TK_GT, // > + TK_GTEQ, // >= + TK_LT, // < + TK_LTEQ, // <= + TK_EQEQ, // == + TK_NOTEQ, // != + + TK_LEFT_PAREN, // ( + TK_RIGHT_PAREN, // ) + TK_LEFT_BRACKET, // [ + TK_RIGHT_BRACKET, // ] + TK_LEFT_BRACE, // { + TK_RIGHT_BRACE, // } + + TK_CLASS, // class + TK_EXTENDS, // extends + TK_NEW, // new + TK_SELF, // self + TK_TYPE, // type + TK_INTERNAL, // internal + TK_FUNCTION, // function + TK_RETURN, // return + TK_STATIC, // static + TK_VAR, // var + TK_BASE, // base + TK_IMPORT, // import + TK_AS, // as + TK_IF, // if + TK_ELSEIF, // elseif + TK_ELSE, // else + TK_FOR, // for + TK_FOREACH, // foreach + TK_IN, // in + TK_CONTINUE, // continue + TK_BREAK, // break + TK_WHILE, // while + TK_DO, // do + TK_UNTIL, // until + TK_ENUM, // enum + TK_MASK, // mask + TK_SWITCH, // switch + TK_CASE, // case + TK_DEFAULT, // default + TK_GOTO, // goto + TK_TRUE, // true + TK_FALSE, // false + + TK_TYPEOF, // typeof + TK_INSTANCEOF, // instanceof + + TK_IDENTIFIER, // <identifier> a-z A-Z 0-9 _ + TK_INT, // <int value> 0x 0c 0b + TK_DOUBLE, // <double value> + TK_STRING, // <string value> + + TK_EOF, // end of file + +} Token; + +#endif
\ No newline at end of file diff --git a/src/nb_undump.c b/src/nb_undump.c new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/nb_undump.c diff --git a/src/nb_undump.h b/src/nb_undump.h new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/nb_undump.h diff --git a/src/nb_util.c b/src/nb_util.c new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/nb_util.c diff --git a/src/nb_util.h b/src/nb_util.h new file mode 100644 index 0000000..10ea124 --- /dev/null +++ b/src/nb_util.h @@ -0,0 +1,7 @@ +#ifndef NB_UTIL_H +#define NB_UTIL_H + +#include <assert.h> +#include <stdbool.h> + +#endif
\ No newline at end of file diff --git a/src/nb_value.c b/src/nb_value.c new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/nb_value.c diff --git a/src/nb_value.h b/src/nb_value.h new file mode 100644 index 0000000..b8191bb --- /dev/null +++ b/src/nb_value.h @@ -0,0 +1,20 @@ +#ifndef NB_VALUE_H +#define NB_VALUE_H + +// Identifies which specific type a heap-allocated object is. +typedef enum { + OBJ_CLASS, + OBJ_CLOSURE, + OBJ_FIBER, + OBJ_FN, + OBJ_FOREIGN, + OBJ_INSTANCE, + OBJ_LIST, + OBJ_MAP, + OBJ_MODULE, + OBJ_RANGE, + OBJ_STRING, + OBJ_UPVALUE +} ObjType; + +#endif
\ No newline at end of file diff --git a/src/nb_vm.c b/src/nb_vm.c new file mode 100644 index 0000000..9bf7a2c --- /dev/null +++ b/src/nb_vm.c @@ -0,0 +1,6 @@ +#include "nb_vm.h" + +void nb_collect_garbage(NbVM* vm) { + +} + diff --git a/src/nb_vm.h b/src/nb_vm.h new file mode 100644 index 0000000..50666af --- /dev/null +++ b/src/nb_vm.h @@ -0,0 +1,14 @@ +#ifndef NB_VM_H +#define NB_VM_H + +typedef struct +{ + int pc; +} NbVM; + + +void nb_collect_garbage(NbVM* vm); + + + +#endif
\ No newline at end of file diff --git a/src/nbc.c b/src/nbc.c new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/nbc.c diff --git a/src/noob.h b/src/noob.h new file mode 100644 index 0000000..b8f4035 --- /dev/null +++ b/src/noob.h @@ -0,0 +1,12 @@ +#ifndef NB_H +#define NB_H + +typedef struct NbVM NbVM; + +typedef int(*NbCFunction) (NbVM* vm); + +typedef double NbDouble; +typedef int NbInt; +typedef int NbBool; + +#endif
\ No newline at end of file diff --git a/src/noob.hpp b/src/noob.hpp new file mode 100644 index 0000000..d15799f --- /dev/null +++ b/src/noob.hpp @@ -0,0 +1,6 @@ +#ifndef NB_HPP +#define NB_HPP + + + +#endif
\ No newline at end of file |