Skip to content

Commit

Permalink
<feature> add: DFA_Import
Browse files Browse the repository at this point in the history
  • Loading branch information
jackutea committed Jan 10, 2024
1 parent 0956cb9 commit e166743
Show file tree
Hide file tree
Showing 16 changed files with 121 additions and 67 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"d_dfa_struct.h": "c",
"m_dfa_struct.h": "c",
"compiler.h": "c",
"stdbool.h": "c"
"stdbool.h": "c",
"type_traits": "c"
}
}
2 changes: 1 addition & 1 deletion run.bat
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@echo off
call build.bat
.\bin\lingc.exe
lingc.exe -i samples\import
@echo on
2 changes: 2 additions & 0 deletions samples/import/import.ling
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import <stdio.h>;
import "raylib.h";
25 changes: 13 additions & 12 deletions src/Business/B_Tokenize.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ void B_Tokenize_SeqMove(E_Doc *doc, const string filename, const string code, lo
E_Doc_Init(doc, filename);

M_NFA_Top *nfa_top = calloc(1, sizeof(M_NFA_Top));
M_NFA_Top_Init(nfa_top);
int index = 0;

while (index < size) {
Expand Down Expand Up @@ -48,19 +49,19 @@ void B_Tokenize_SeqMove(E_Doc *doc, const string filename, const string code, lo
D_NFA_Top_Enter(nfa_top);
}
} else if (top_status == NFA_Top_Status_Struct) {
M_DFA_Struct *dfa_struct = nfa_top->dfa_struct;
end_index = D_DFA_Struct_Process(dfa_struct, filename, line, isSplit, word, end_index, code, size);
if (dfa_struct->is_done) {
E_Doc_Struct_Add(doc, dfa_struct->st);
D_NFA_Top_Enter(nfa_top);
}
// M_DFA_Struct *dfa_struct = nfa_top->dfa_struct;
// end_index = D_DFA_Struct_Process(dfa_struct, filename, line, isSplit, word, end_index, code, size);
// if (dfa_struct->is_done) {
// E_Doc_Struct_Add(doc, dfa_struct->st);
// D_NFA_Top_Enter(nfa_top);
// }
} else if (top_status == NFA_Top_Status_Func) {
M_DFA_Func *fsm_func = nfa_top->dfa_func;
end_index = D_DFA_Func_Process(fsm_func, filename, line, isSplit, word, end_index, code, size);
if (fsm_func->is_done) {
E_Doc_StaticFunc_Add(doc, fsm_func->function);
D_NFA_Top_Enter(nfa_top);
}
// M_DFA_Func *fsm_func = nfa_top->dfa_func;
// end_index = D_DFA_Func_Process(fsm_func, filename, line, isSplit, word, end_index, code, size);
// if (fsm_func->is_done) {
// E_Doc_StaticFunc_Add(doc, fsm_func->function);
// D_NFA_Top_Enter(nfa_top);
// }
} else if (top_status == NFA_Top_Status_Guess) {
D_NFA_Top_Process(nfa_top, filename, line, isSplit, word, code, size);
}
Expand Down
40 changes: 22 additions & 18 deletions src/Business/D_DFA_Import.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "import.h"

void D_DFA_Import_Free(M_DFA_Import *fsm) {
PLogNA("free top import\r\n");
}

void D_DFA_Import_Enter(M_DFA_Import *fsm) {
Expand All @@ -13,38 +12,43 @@ void D_DFA_Import_Enter(M_DFA_Import *fsm) {

int D_DFA_Import_Process(M_DFA_Import *fsm, const string file, int line, bool is_split, const string word, int index, const string code, long size) {

if (is_split && Char_IsEmptySymbol(word[0])) {
if (!is_split) {
return index;
}

if (Char_IsEmptySymbol(word[0])) {
return index;
}

char split = word[0];
const string value;
ImportType type;
if (split == KW_QUOTE) {
// "
value = String_CutBetweenSameChars(index, code, size, KW_QUOTE);
if (value == NULL) {
printf("err word:%s\r\n", word);
PFailed(file, line, ERR_UNDIFINDED_ERR);
int right_index = String_CutBetweenSameChars(index, code, size, KW_QUOTE);
if (right_index != -1) {
const string value = String_SubString(code, index + 1, right_index - index - 1); // 去掉两边的引号
fsm->import = Factory_CreateImport(value, ImportType_Quote);
index = right_index;
} else {
type = ImportType_Quote;
PLog("err word:%s\r\n", word);
PFailed(file, line, ERR_UNDIFINDED_ERR);
}
} else if (split == KW_LEFT_ANGLE_BRACKET) {
// <
value = String_CutBetweenDifferentChars(index, code, size, KW_LEFT_ANGLE_BRACKET, KW_RIGHT_ANGLE_BRACKET);
if (value == NULL) {
PFailed(file, line, ERR_UNDIFINDED_ERR);
int right_index = String_CutBetweenDifferentChars(index, code, size, KW_LEFT_ANGLE_BRACKET, KW_RIGHT_ANGLE_BRACKET);
if (right_index != -1) {
const string value = String_SubString(code, index + 1, right_index - index - 1); // 去掉两边的尖括号
fsm->import = Factory_CreateImport(value, ImportType_ANGLE_BRACKET);
index = right_index;
} else {
type = ImportType_ANGLE_BRACKET;
PLog("err word:%s\r\n", word);
PFailed(file, line, ERR_UNDIFINDED_ERR);
}
} else if (split == KW_SEMICOLON) {
// ;
fsm->is_done = true;
}

if (value != NULL) {
fsm->import = Factory_CreateImport(value, type);
if (split == KW_SEMICOLON) {
// ;
fsm->is_done = true;
}

return index;
}
22 changes: 11 additions & 11 deletions src/Business/D_NFA_Top.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
#include "D_DFA_Struct.h"

void D_NFA_Top_Free(M_NFA_Top *nfa_top) {
D_DFA_Func_Free(nfa_top->dfa_func);
D_DFA_Import_Free(nfa_top->dfa_import);
D_DFA_Struct_Free(nfa_top->dfa_struct);
free(nfa_top);
M_NFA_Top_Free(nfa_top);
}

void D_NFA_Top_Enter(M_NFA_Top *nfa_top) {
nfa_top->status = NFA_Top_Status_Guess;
E_Guess_Init(&nfa_top->guess);
}

void D_NFA_Top_Process(M_NFA_Top *nfa_top, const string file, int line, bool is_split, const string word, const string code, long size) {

if (is_split) {
return;
if (Char_IsEmptySymbol(word[0])) {
return;
}
}

E_Guess *guess = &nfa_top->guess;
Expand All @@ -29,14 +29,14 @@ void D_NFA_Top_Process(M_NFA_Top *nfa_top, const string file, int line, bool is_
D_DFA_Import_Enter(dfa_import);
} else if (strcmp(word, KW_FUNC) == 0) {
// fn
nfa_top->status = NFA_Top_Status_Func;
M_DFA_Func *dfa_func = nfa_top->dfa_func;
D_DFA_Func_Enter(dfa_func, guess);
// nfa_top->status = NFA_Top_Status_Func;
// M_DFA_Func *dfa_func = nfa_top->dfa_func;
// D_DFA_Func_Enter(dfa_func, guess);
} else if (strcmp(word, KW_STRUCT) == 0) {
// struct
nfa_top->status = NFA_Top_Status_Struct;
M_DFA_Struct *dfa_struct = nfa_top->dfa_struct;
D_DFA_Struct_Enter(dfa_struct, file, line, guess);
// nfa_top->status = NFA_Top_Status_Struct;
// M_DFA_Struct *dfa_struct = nfa_top->dfa_struct;
// D_DFA_Struct_Enter(dfa_struct, file, line, guess);
} else if (strcmp(word, KW_STATIC) == 0) {
// static
guess->is_static = true;
Expand Down
4 changes: 3 additions & 1 deletion src/Compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ void Compile(const char *root) {
long size = File_ReadAllText(fp, &str);

// doc
PLog("compile file: %s\r\n", files[i]);
PLog("file begin: %s\r\n", files[i]);
E_Doc *doc = calloc(1, sizeof(E_Doc));
Context_AddDoc(ctx, doc);

// tokenize
B_Tokenize_SeqMove(doc, files[i], str, size);

PLog("file done: %s\r\n", files[i]);

fclose(fp);
free(str);
}
Expand Down
20 changes: 16 additions & 4 deletions src/Entities_Source/E_Doc.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,37 @@ void E_Doc_Init(E_Doc *doc, const string file) {

void E_Doc_Free(E_Doc *doc) {

// - Import
for (int i = 0; i < doc->imports_count; i++) {
// E_Import_Free(&doc->imports[i]);
}
free(doc->imports);
if (doc->imports_count > 0) {
free(doc->imports);
}

// - Struct
for (int i = 0; i < doc->structs_count; i++) {
E_Struct_Free(&doc->structs[i]);
}
free(doc->structs);
if (doc->structs_count > 0) {
free(doc->structs);
}

// - StaticFunc
for (int i = 0; i < doc->static_funcs_count; i++) {
E_Function_Free(&doc->static_funcs[i]);
}
free(doc->static_funcs);
if (doc->static_funcs_count > 0) {
free(doc->static_funcs);
}

// - StaticVar
for (int i = 0; i < doc->static_vars_count; i++) {
// E_Field_Free(&doc->static_vars[i]);
}
free(doc->static_vars);
if (doc->static_vars_count > 0) {
free(doc->static_vars);
}

free(doc);
}
Expand Down
7 changes: 2 additions & 5 deletions src/Entities_Source/M_DFA_Import.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
#define M_DFA_IMPORT_H__

typedef struct M_DFA_Import {
bool isEntering;
int leftIndex;
int rightIndex;
ImportType importType;
bool is_done;
E_Import import;
bool is_recorded;
bool is_done;
} M_DFA_Import;

void M_DFA_Import_Init(M_DFA_Import *fsm);
Expand Down
8 changes: 8 additions & 0 deletions src/Entities_Source/M_DFA_Struct.c
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
#include "M_DFA_Struct.h"

void M_DFA_Struct_Init(M_DFA_Struct *dfa_struct) {
memset(dfa_struct, 0, sizeof(M_DFA_Struct));
}

void M_DFA_Struct_Free(M_DFA_Struct *dfa_struct) {
free(dfa_struct);
}
3 changes: 3 additions & 0 deletions src/Entities_Source/M_DFA_Struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ typedef struct M_DFA_Struct {
bool is_done;
} M_DFA_Struct;

void M_DFA_Struct_Init(M_DFA_Struct *dfa_struct);
void M_DFA_Struct_Free(M_DFA_Struct *dfa_struct);

#endif
17 changes: 16 additions & 1 deletion src/Entities_Source/M_NFA_Top.c
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
#include "M_NFA_Top.h"
#include "M_NFA_Top.h"

void M_NFA_Top_Init(M_NFA_Top *nfa_top) {
nfa_top->status = NFA_Top_Status_Guess;
nfa_top->dfa_struct = calloc(1, sizeof(M_DFA_Struct));
nfa_top->dfa_import = calloc(1, sizeof(M_DFA_Import));
nfa_top->dfa_func = calloc(1, sizeof(M_DFA_Func));
E_Guess_Init(&nfa_top->guess);
}

void M_NFA_Top_Free(M_NFA_Top *nfa_top) {
M_DFA_Func_Free(nfa_top->dfa_func);
M_DFA_Import_Free(nfa_top->dfa_import);
M_DFA_Struct_Free(nfa_top->dfa_struct);
free(nfa_top);
}
3 changes: 3 additions & 0 deletions src/Entities_Source/M_NFA_Top.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ typedef struct M_NFA_Top {
E_Guess guess;
} M_NFA_Top;

void M_NFA_Top_Init(M_NFA_Top *nfa_top);
void M_NFA_Top_Free(M_NFA_Top *nfa_top);

#endif
14 changes: 4 additions & 10 deletions src/Generic/StringCommon.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ inline const string String_SubString(const string word, int start_index, int end
return TextSubtext(word, start_index, end_index);
}

const string String_CutBetweenSameChars(int start_index, const string code, long size, char same_area) {
int String_CutBetweenSameChars(int start_index, const string code, long size, char same_area) {
int left_index = -1;
int right_index = -1;
for (int i = start_index; i < size; i++) {
Expand All @@ -335,13 +335,10 @@ const string String_CutBetweenSameChars(int start_index, const string code, long
}
}
}
if (left_index == -1 || right_index == -1) {
return NULL;
}
return String_SubString(code, left_index + 1, right_index - left_index - 1);
return right_index;
}

const string String_CutBetweenDifferentChars(int start_index, const string word, long size, char left, char right) {
int String_CutBetweenDifferentChars(int start_index, const string word, long size, char left, char right) {
int left_index = -1;
int right_index = -1;
for (int i = start_index; i < size; i++) {
Expand All @@ -353,10 +350,7 @@ const string String_CutBetweenDifferentChars(int start_index, const string word,
break;
}
}
if (left_index == -1 || right_index == -1) {
return NULL;
}
return String_SubString(word, left_index + 1, right_index - left_index - 1);
return right_index;
}

void String_CopyAccess(char *dest, const string access) {
Expand Down
4 changes: 2 additions & 2 deletions src/Generic/StringCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ int String_OP_Assign(const string file, int line, int eqIndex, const string code
int String_OP_Calc(const char cur, const string file, int line, int eqIndex, const string code, char *out);
int String_OP_CalcOrCommaOrMember(const char cur, const string file, int line, int eqIndex, const string code, char *out);
const string String_SubString(const string word, int start_index, int end_index);
const string String_CutBetweenSameChars(int start_index, const string word, long size, char same_area);
const string String_CutBetweenDifferentChars(int start_index, const string word, long size, char left, char right);
int String_CutBetweenSameChars(int start_index, const string word, long size, char same_area);
int String_CutBetweenDifferentChars(int start_index, const string word, long size, char left, char right);
bool String_IsAccess(const string word);
char Char_IsBracket(char c);
char Char_IsQuote(char c);
Expand Down
14 changes: 13 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,20 @@ int main(int argc, char **argv) {

StringCommon_Init();


// lingc.exe -i ../tests/src
const string dir = GetWorkingDirectory();
const string root = TextFormat("%s\\%s\\%s", dir, "tests", "src");
if (argc < 3) {
printf("Usage: lingc.exe -i <input directory>\n");
return 0;
}

if (strcmp(argv[1], "-i") != 0) {
printf("Usage: lingc.exe -i <input directory>\n");
return 0;
}

const string root = TextFormat("%s\\%s", dir, argv[2]);

Compile(root);

Expand Down

0 comments on commit e166743

Please sign in to comment.