mirror of
https://github.com/pocketpy/pocketpy
synced 2025-12-07 10:40:16 +00:00
some fix
This commit is contained in:
parent
79b9df3392
commit
1ac08cfc2b
15
include/pocketpy/compiler/compiler.h
Normal file
15
include/pocketpy/compiler/compiler.h
Normal 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
|
||||||
@ -35,8 +35,15 @@ typedef enum TokenIndex{
|
|||||||
TK__COUNT__
|
TK__COUNT__
|
||||||
} TokenIndex;
|
} TokenIndex;
|
||||||
|
|
||||||
|
enum TokenValueIndex{
|
||||||
|
TokenValue_EMPTY = 0,
|
||||||
|
TokenValue_I64 = 1,
|
||||||
|
TokenValue_F64 = 2,
|
||||||
|
TokenValue_STR = 3,
|
||||||
|
};
|
||||||
|
|
||||||
typedef struct TokenValue {
|
typedef struct TokenValue {
|
||||||
int index; // 0: empty
|
enum TokenValueIndex index; // 0: empty
|
||||||
union {
|
union {
|
||||||
int64_t _i64; // 1
|
int64_t _i64; // 1
|
||||||
double _f64; // 2
|
double _f64; // 2
|
||||||
@ -44,11 +51,6 @@ typedef struct TokenValue {
|
|||||||
};
|
};
|
||||||
} TokenValue;
|
} TokenValue;
|
||||||
|
|
||||||
#define TokenValue_EMPTY 0
|
|
||||||
#define TokenValue_I64 1
|
|
||||||
#define TokenValue_F64 2
|
|
||||||
#define TokenValue_STR 3
|
|
||||||
|
|
||||||
typedef struct Token {
|
typedef struct Token {
|
||||||
TokenIndex type;
|
TokenIndex type;
|
||||||
const char* start;
|
const char* start;
|
||||||
|
|||||||
20
src/compiler/compiler.c
Normal file
20
src/compiler/compiler.c
Normal 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;
|
||||||
|
}
|
||||||
@ -814,16 +814,16 @@ Error* pk_Lexer__process_and_dump(pk_SourceData_ src, py_Str* out) {
|
|||||||
}
|
}
|
||||||
// visit token value
|
// visit token value
|
||||||
switch(token->value.index){
|
switch(token->value.index){
|
||||||
case 0: break;
|
case TokenValue_EMPTY: break;
|
||||||
case 1:
|
case TokenValue_I64:
|
||||||
pk_SStream__write_char(&ss, 'I');
|
pk_SStream__write_char(&ss, 'I');
|
||||||
pk_SStream__write_int(&ss, token->value._i64);
|
pk_SStream__write_int(&ss, token->value._i64);
|
||||||
break;
|
break;
|
||||||
case 2:
|
case TokenValue_F64:
|
||||||
pk_SStream__write_char(&ss, 'F');
|
pk_SStream__write_char(&ss, 'F');
|
||||||
pk_SStream__write_float(&ss, token->value._f64, -1);
|
pk_SStream__write_float(&ss, token->value._f64, -1);
|
||||||
break;
|
break;
|
||||||
case 3: {
|
case TokenValue_STR: {
|
||||||
pk_SStream__write_char(&ss, 'S');
|
pk_SStream__write_char(&ss, 'S');
|
||||||
c11_string sv = py_Str__sv(&token->value._str);
|
c11_string sv = py_Str__sv(&token->value._str);
|
||||||
for(int i=0; i<sv.size; i++){
|
for(int i=0; i<sv.size; i++){
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user