#define l_lsl_lex_advance(v, code) \
(code)[++v->lex_pos]
+#define l_lsl_lex_term_token(tokbuf, token_len) \
+ tokbuf[*token_len] = '\0';
char tokbuf[L_LSL_TOK_MAX];
-const char* l_lsl_next_token(struct l_lsl_vm *v, const char *code,
+struct l_lsl_value* l_lsl_next_token(struct l_lsl_vm *v, const char *code,
u32 *token_len) {
char c = 0;
+ struct l_lsl_value *value = l_lsl_value_alloc(v);
*token_len = 0;
c = l_lsl_lex_peek(v, code);
if (l_lsl_isnum(c)) {
+ value->type = L_LSL_TYPE_INT;
/* decimal number */
while (l_lsl_isnum(c)) {
tokbuf[*token_len] = c;
*token_len += 1;
c = l_lsl_lex_advance(v, code);
}
+ l_lsl_lex_term_token(tokbuf, token_len);
} else if (c == '0') {
+ value->type = L_LSL_TYPE_INT;
/* specific base number */
c = l_lsl_lex_advance(v, code);
return LRTS_NULL;
}
- tokbuf[*token_len] = '\0';
- return tokbuf;
+ /* now we have a token
+ * return a value type based
+ * on the token type
+ */
+
+ return value;
}
struct l_lsl_value* l_lsl_value_alloc(struct l_lsl_vm *v) {
struct l_lsl_value *val;
val = v->malloc(sizeof(*val));
+
+ lrts_assert(v);
+ lrts_assert(val);
+
*val = l_lsl_value_init();
return val;
}
-struct l_lsl_value l_lsl_compile_file(struct l_lsl_vm *v, const char *path) {
- struct l_lsl_value val = l_lsl_value_init();
- const char *src = u_file_read(path);
+void l_lsl_value_free(struct l_lsl_vm *v,
+ struct l_lsl_value *val) {
+ lrts_assert(v);
+ lrts_assert(val);
+
+ v->free(val);
+}
+
+struct l_lsl_value* l_lsl_compile(struct l_lsl_vm *v, const char *code,
+ u32 len) {
+ struct l_lsl_value *val = LRTS_NULL;
u32 token_len;
- const char *token;
+ /* TODO: length check */
+ LRTS_UNUSED(len);
+
+
+ val = l_lsl_next_token(v, code, &token_len);
+
+ return val;
+}
+
+struct l_lsl_value* l_lsl_compile_file(struct l_lsl_vm *v, const char *path) {
+ const char *src = u_file_read(path);
+ struct l_lsl_value *val = LRTS_NULL;
+
u32 prev_lex_pos = v->lex_pos;
const char *prev_file_name = v->path;
return val;
}
-
- token = l_lsl_next_token(v, src, &token_len);
- u_printf("%s\n", token);
+ val = l_lsl_compile(v, src, u_strnlen(src, L_LSL_SOURCE_MAX));
u_free((void*)src);
return val;
}
-struct l_lsl_value l_lsl_run(struct l_lsl_vm *v, struct l_lsl_value *program) {
- struct l_lsl_value val = l_lsl_value_init();
+struct l_lsl_value* l_lsl_run(struct l_lsl_vm *v, struct l_lsl_value *program) {
+ struct l_lsl_value *val = LRTS_NULL;
LRTS_UNUSED(v);
- LRTS_UNUSED(program);
+ LRTS_UNUSED(val);
- return val;
+ return program;
}
-struct l_lsl_value l_lsl_exec(struct l_lsl_vm *v, const char *path) {
- struct l_lsl_value val = l_lsl_value_init();
-
- l_lsl_compile_file(v, path);
+struct l_lsl_value* l_lsl_exec(struct l_lsl_vm *v, const char *path) {
+ struct l_lsl_value *val = l_lsl_compile_file(v, path);
if (v->err) {
return val;
}
- l_lsl_run(v, LRTS_NULL);
-
- return val;
+ return l_lsl_run(v, val);
}
struct l_lsl_value l_lsl_value_init(void);
-/* frees a value if rc == 0
- * if rc != 0 decreases rc by one
- */
-void l_lsl_value_free(struct l_lsl_value *v);
+struct l_lsl_value* l_lsl_value_alloc(struct l_lsl_vm *v);
+
+void l_lsl_value_free(struct l_lsl_vm *v,
+ struct l_lsl_value *val);
/*
* gets the next token returns tone's length in token_len
* the result token is placed into an internal string buffer and should not be modified
* or saved.
*/
-const char* l_lsl_next_token(struct l_lsl_vm *v, const char *code, u32 *token_len);
+struct l_lsl_value* l_lsl_next_token(struct l_lsl_vm *v, const char *code,
+ u32 *token_len);
/* compiles a program
* returns the resulting program's head as a list
*/
-struct l_lsl_value l_lsl_compile(struct l_lsl_vm *v, const char *code, u32 len);
+struct l_lsl_value* l_lsl_compile(struct l_lsl_vm *v, const char *code, u32 len);
/* runs a program from a given entry point function
* if the entry point is NULL the global scope is executed */
-struct l_lsl_value l_lsl_run(struct l_lsl_vm *v, struct l_lsl_value *program);
+struct l_lsl_value* l_lsl_run(struct l_lsl_vm *v, struct l_lsl_value *program);
/* compiles from a file */
-struct l_lsl_value l_lsl_compile_file(struct l_lsl_vm *v, const char *path);
+struct l_lsl_value* l_lsl_compile_file(struct l_lsl_vm *v, const char *path);
/* loads a file, compiles it and runs it */
-struct l_lsl_value l_lsl_exec(struct l_lsl_vm *v, const char *path);
+struct l_lsl_value* l_lsl_exec(struct l_lsl_vm *v, const char *path);
#endif
int t_lsl_assert(struct l_lsl_value expect_value, enum l_lsl_error expect_err,
const char *src_file) {
struct l_lsl_vm v = l_lsl_vm_init();
- struct l_lsl_value res = l_lsl_exec(&v, src_file);
+ struct l_lsl_value *res = l_lsl_exec(&v, src_file);
enum l_lsl_error err = v.err;
l_lsl_vm_free(&v);
-
+
+ T_ASSERT(res != LRTS_NULL, ("Invalid result value"))
T_ASSERT(err == expect_err, ("Unexpected error code %d\n", err));
- T_ASSERT(res.type == expect_value.type, ("Unexpected return value"));
-
+ T_ASSERT(res->type == expect_value.type, ("Unexpected return value\n"));
+
+ l_lsl_value_free(&v, res);
return 0;
}
int t_test_lsl() {
struct l_lsl_value v = l_lsl_value_init();
-
+
+ v.type = L_LSL_TYPE_INT;
T_LSL_ASSERT(v, L_LSL_ERR_OK, "./src/tests/lsl/intliteral.lsl");
return 0;
}