This commit is contained in:
blueloveTH 2024-06-28 15:47:11 +08:00
parent 79b9df3392
commit 1ac08cfc2b
4 changed files with 47 additions and 10 deletions

View File

@ -0,0 +1,15 @@
#pragma once
#include "pocketpy/common/vector.h"
#include "pocketpy/compiler/lexer.h"
#ifdef __cplusplus
extern "C" {
#endif
Error* pk_compile(pk_SourceData_ src);
#ifdef __cplusplus
}
#endif

View File

@ -35,8 +35,15 @@ typedef enum TokenIndex{
TK__COUNT__
} TokenIndex;
enum TokenValueIndex{
TokenValue_EMPTY = 0,
TokenValue_I64 = 1,
TokenValue_F64 = 2,
TokenValue_STR = 3,
};
typedef struct TokenValue {
int index; // 0: empty
enum TokenValueIndex index; // 0: empty
union {
int64_t _i64; // 1
double _f64; // 2
@ -44,11 +51,6 @@ typedef struct TokenValue {
};
} TokenValue;
#define TokenValue_EMPTY 0
#define TokenValue_I64 1
#define TokenValue_F64 2
#define TokenValue_STR 3
typedef struct Token {
TokenIndex type;
const char* start;

20
src/compiler/compiler.c Normal file
View File

@ -0,0 +1,20 @@
#include "pocketpy/compiler/compiler.h"
Error* pk_compile(pk_SourceData_ src){
c11_array/*T=Token*/ tokens;
Error* err = pk_Lexer__process(src, &tokens);
if(err) return err;
Token* data = (Token*)tokens.data;
printf("%s\n", py_Str__data(&src->filename));
for(int i = 0; i < tokens.count; i++) {
Token* t = data + i;
py_Str tmp;
py_Str__ctor2(&tmp, t->start, t->length);
printf("[%d] %s: %s\n", t->line, pk_TokenSymbols[t->type], py_Str__data(&tmp));
py_Str__dtor(&tmp);
}
c11_array__dtor(&tokens);
return NULL;
}

View File

@ -814,16 +814,16 @@ Error* pk_Lexer__process_and_dump(pk_SourceData_ src, py_Str* out) {
}
// visit token value
switch(token->value.index){
case 0: break;
case 1:
case TokenValue_EMPTY: break;
case TokenValue_I64:
pk_SStream__write_char(&ss, 'I');
pk_SStream__write_int(&ss, token->value._i64);
break;
case 2:
case TokenValue_F64:
pk_SStream__write_char(&ss, 'F');
pk_SStream__write_float(&ss, token->value._f64, -1);
break;
case 3: {
case TokenValue_STR: {
pk_SStream__write_char(&ss, 'S');
c11_string sv = py_Str__sv(&token->value._str);
for(int i=0; i<sv.size; i++){