Compare commits

...
32 Commits
Author SHA1 Message Date
bedohswe 9bb5280c44 Do not add spaces between dots. 2025-01-29 20:23:58 +05:00
bedohswe cfea07531d Fix memory leak. 2025-01-29 19:00:31 +05:00
bedohswe 9fdfe76804 C moment. 2025-01-29 18:44:02 +05:00
bedohswe 635747af1d Spaces 2024-06-12 21:35:24 +05:00
bedohswe d6af70056a Fix if statements 2024-06-11 21:55:49 +05:00
bedohswe 9addce5f30 Add a debug switch 2024-06-11 21:31:30 +05:00
bedohswe 07402a6b42 Add "range" 2024-06-11 21:26:39 +05:00
bedohswe 9b605597b2 Add "is" 2024-06-11 20:38:19 +05:00
bedohswe 8eff72ee53 Fix compilation error 2024-06-11 19:58:49 +05:00
bedohswe b98579699d Improve #def 2024-06-11 19:58:09 +05:00
bedohswe 74d20d9f37 Add some more keywords 2024-06-11 19:44:40 +05:00
bedohswe 13bb7399aa Add "filter" 2024-06-11 19:10:46 +05:00
bedohswe 5edcf05d7a Add "break" and "continue" 2024-06-11 19:00:24 +05:00
bedohswe 74bf888a84 Add for loops 2024-06-11 18:55:20 +05:00
bedohswe 099bb1d600 Add "use" 2024-06-11 18:28:50 +05:00
bedohswe d30a715d06 Add "assert" and "fn" 2024-06-11 18:26:23 +05:00
bedohswe c8189f5ec1 Implement verbatim 2024-06-11 18:00:16 +05:00
bedohswe c543c00a9c Fix memory leaks 2024-06-11 17:38:59 +05:00
bedohswe b762ad154e Formatting 2024-06-11 17:34:00 +05:00
bedohswe a8e4bfd264 Implement maps 2024-06-11 17:33:41 +05:00
bedohswe 834ec9d3d4 Fix list declarations 2024-06-11 16:54:41 +05:00
bedohswe e6027bbaf2 Allow print to take multiple arguments 2024-06-11 16:53:40 +05:00
bedohswe 3aa280ba38 Implement list declarations 2024-06-11 16:51:16 +05:00
bedohswe 898fa9b195 How in the bloody hell it end up like that!? 2024-06-11 16:29:40 +05:00
bedohswe 71ee935f7b Rename def to #def 2024-06-11 16:15:09 +05:00
bedohswe 1c9de73b91 Implement longer defines 2024-06-11 16:14:18 +05:00
bedohswe 5cc866637e Add back preprocessing 2024-06-11 16:07:09 +05:00
bedohswe ab7f648e56 Notify of unknown keywords 2024-06-11 16:06:47 +05:00
bedohswe 57af99e53a Change formating a bit 2024-06-11 16:02:51 +05:00
bedohswe d5ccf1c0be Add :? as an operator 2024-06-11 16:00:32 +05:00
bedohswe a1d14e8cfa Add "neg" and "not" keywords 2024-06-11 15:59:53 +05:00
bedohswe 8bf0af96ee Add get keyword 2024-06-11 05:48:18 +05:00
6 changed files with 442 additions and 68 deletions
+378 -50
View File
@@ -13,6 +13,36 @@ void string_append(char** string, char* append) {
}
void string_append_char(char** string, char append) {
int len = strlen(*string);
*string = realloc(*string, len + 2 * sizeof(char));
string[0][len] = append;
string[0][len + 1] = '\0';
}
char* string_unescape(char* string) {
char* tmp = calloc(1,sizeof(char));
size_t len = strlen(string);
for (size_t i = 0; i < len; i++) {
if (string[i] == '\\')
i++;
string_append_char(&tmp, string[i]);
}
return tmp;
}
void string_append_free(char** string, char* append) {
string_append(string, append);
free(append);
}
void compile_enter_tag(char** string) {
int len = strlen(*string);
@@ -58,6 +88,7 @@ bool compile_isop(char* string) {
else if (strcmp(string, "and") == 0) return true;
else if (strcmp(string, "or") == 0) return true;
else if (strcmp(string, "|") == 0) return true;
else if (strcmp(string, ":?") == 0) return true;
else return false;
@@ -91,7 +122,7 @@ void compile_genexec(char** result, tSyntaxElement* se){
if (token != NULL) {
string_append(result,compile_expression(token));
string_append_free(result,compile_expression(token));
if (token->next != NULL)
string_append(result,",");
@@ -105,6 +136,37 @@ void compile_genexec(char** result, tSyntaxElement* se){
string_append(result,")");
}
char* compile_gensplit(tSyntaxElement* se, char* operator, bool dosplit) {
tSyntaxElement* token = se;
char* result = malloc(sizeof(char));
result[0] = '\0';
while (1) {
if (token->next != NULL) {
if (token->next->type == TOKEN)
string_append(&result,token->next->content.string);
else
string_append_free(&result,compile_expression_wrapped(token->next));
if (token->next->next != NULL) {
if (dosplit)
string_append(&result," ");
string_append(&result,operator);
if (dosplit)
string_append(&result," ");
}
token = token->next;
} else break;
}
return result;
}
char* compile_expression(tSyntaxElement* syntaxelement) {
char* result = calloc(1,sizeof(char));
@@ -138,38 +200,113 @@ char* compile_expression(tSyntaxElement* syntaxelement) {
char* operator = token->content.string;
while (1) {
if (token->next != NULL) {
if (token->next->type == TOKEN)
string_append(&result,token->next->content.string);
else
string_append(&result,compile_expression_wrapped(token->next));
if (token->next->next != NULL)
string_append(&result,operator);
token = token->next;
} else break;
}
string_append_free(&result, compile_gensplit(token, operator, !!strcmp(".", token->content.string)));
} else {
if (strcmp(token->content.string, "if") == 0) {
string_append(&result,compile_expression_wrapped(token->next));
string_append_free(&result,compile_expression_wrapped(token->next));
string_append(&result,"?");
string_append(&result,compile_expression_wrapped(token->next->next));
string_append_free(&result,compile_expression_wrapped(token->next->next));
string_append(&result,":");
string_append(&result,compile_expression_wrapped(token->next->next->next));
string_append_free(&result,compile_expression_wrapped(token->next->next->next));
}
else {
} else if (strcmp(token->content.string, "get") == 0) {
string_append_free(&result,compile_expression(token->next));
string_append(&result,"[");
string_append_free(&result,compile_expression(token->next->next));
string_append(&result,"]");
} else if (strcmp(token->content.string, "neg") == 0) {
string_append(&result,"-");
string_append_free(&result,compile_expression_wrapped(token->next));
} else if (strcmp(token->content.string, "not") == 0) {
string_append(&result,"not");
string_append_free(&result,compile_expression_wrapped(token->next));
} else if (strcmp(token->content.string, "list") == 0) {
string_append(&result,"[");
string_append_free(&result,compile_gensplit(token, ",", true));
string_append(&result,"]");
} else if (strcmp(token->content.string, "range") == 0) {
string_append(&result,"[");
string_append_free(&result,compile_gensplit(token, "..", true));
string_append(&result,"]");
} else if (strcmp(token->content.string, "map") == 0) {
string_append(&result,"{");
for (tSyntaxElement* i = token->next; i != NULL; i = i->next) {
string_append_free(&result, compile_expression(i->content.syntax));
string_append(&result, ":");
string_append_free(&result, compile_expression(i->content.syntax->next));
if (i->next != NULL)
string_append(&result,",");
}
string_append(&result,"}");
} else if (strcmp(token->content.string, "isnull") == 0) {
string_append_free(&result, compile_expression_wrapped(token->next));
string_append(&result, " is null");
} else if (strcmp(token->content.string, "notnull") == 0) {
string_append_free(&result, compile_expression_wrapped(token->next));
string_append(&result, " is not null");
} else if (strcmp(token->content.string, "same") == 0) {
string_append_free(&result, compile_expression_wrapped(token->next));
string_append(&result, " is same as ");
string_append_free(&result, compile_expression_wrapped(token->next->next));
} else if (strcmp(token->content.string, "diff") == 0) {
string_append_free(&result, compile_expression_wrapped(token->next));
string_append(&result, " is not same as ");
string_append_free(&result, compile_expression_wrapped(token->next->next));
} else if (strcmp(token->content.string, "isdiv") == 0) {
string_append_free(&result, compile_expression_wrapped(token->next));
string_append(&result, " is divisible by ");
string_append_free(&result, compile_expression_wrapped(token->next->next));
} else if (strcmp(token->content.string, "notdiv") == 0) {
string_append_free(&result, compile_expression_wrapped(token->next));
string_append(&result, " is divisible by ");
string_append_free(&result, compile_expression_wrapped(token->next->next));
} else if (strcmp(token->content.string, "is") == 0) {
string_append_free(&result, compile_expression_wrapped(token->next));
string_append(&result, " is ");
string_append_free(&result, compile_expression(token->next->next));
} else if (strcmp(token->content.string, "not") == 0) {
string_append_free(&result, compile_expression_wrapped(token->next));
string_append(&result, " is not ");
string_append_free(&result, compile_expression(token->next->next));
} else {
string_append(&result,token->content.string);
compile_genexec(&result, token);
@@ -182,25 +319,29 @@ char* compile_expression(tSyntaxElement* syntaxelement) {
case TREE: {
tSyntaxElement* token = syntaxelement->content.syntax;
string_append(&result, compile_expression(syntaxelement->content.syntax));
string_append_free(&result, compile_expression(syntaxelement->content.syntax));
compile_genexec(&result, token);
break;
}
case STRING:
return NULL;
case NONE:
case NEWTREE:
return NULL;
string_append(&result, "()");
break;
}
} else {
string_append(&result, "()");
}
break;
case NEWTREE:
case NONE:
free(result);
return NULL;
string_append(&result, "()");
break;
}
@@ -220,56 +361,80 @@ char* compile(tSyntaxElement* syntaxtree) {
tSyntaxElement* token = syntaxtree->content.syntax;
if (strcmp(token->content.string, "def") == 0) {
} else
if (strcmp(token->content.string, "set") == 0) {
if (strcmp(token->content.string, "#def") == 0) {
} else if (strcmp(token->content.string, "set") == 0) {
compile_enter_tag(&result);
string_append(&result, "set ");
string_append(&result,token->next->content.string);
string_append(&result,"=");
string_append(&result,compile_expression_wrapped(token->next->next));
string_append_free(&result,compile_expression_wrapped(token->next->next));
compile_exit_tag(&result);
} else
if (strcmp(token->content.string, "global") == 0) {
} else if (strcmp(token->content.string, "global") == 0) {
compile_enter_tag(&result);
string_append(&result, "global ");
string_append(&result,token->next->content.string);
string_append(&result,"=");
string_append(&result,compile_expression_wrapped(token->next->next));
string_append_free(&result,compile_expression_wrapped(token->next->next));
compile_exit_tag(&result);
} else
if (strcmp(token->content.string, "do") == 0) {
} else if (strcmp(token->content.string, "do") == 0) {
compile_enter_tag(&result);
string_append(&result, "do ");
string_append(&result,compile_expression(token->next));
string_append_free(&result,compile_expression(token->next));
compile_exit_tag(&result);
} else
} else if (strcmp(token->content.string, "use") == 0) {
if (strcmp(token->content.string, "if") == 0) {
compile_enter_tag(&result);
string_append(&result, "use ");
string_append_free(&result,compile_expression(token->next));
compile_exit_tag(&result);
} else if (strcmp(token->content.string, "return") == 0) {
compile_enter_tag(&result);
string_append(&result, "return ");
string_append_free(&result,compile_expression(token->next));
compile_exit_tag(&result);
} else if (strcmp(token->content.string, "run") == 0) {
compile_enter_tag(&result);
string_append(&result, "run ");
string_append_free(&result,compile_expression(token->next));
if (token->next->next != NULL) {
string_append(&result, " with ");
string_append_free(&result,compile_expression(token->next->next));
}
compile_exit_tag(&result);
} else if (strcmp(token->content.string, "if") == 0) {
compile_enter_tag(&result);
string_append(&result, "if ");
string_append(&result,compile_expression(token->next));
string_append_free(&result,compile_expression(token->next));
compile_exit_tag(&result);
string_append(&result,compile(token->next->next));
string_append_free(&result,compile(token->next->next->content.syntax));
if (token->next->next->next != NULL) {
@@ -277,7 +442,7 @@ char* compile(tSyntaxElement* syntaxtree) {
string_append(&result, "else");
compile_exit_tag(&result);
string_append(&result,compile(token->next->next->next));
string_append_free(&result,compile(token->next->next->next->content.syntax));
}
@@ -287,13 +452,176 @@ char* compile(tSyntaxElement* syntaxtree) {
compile_exit_tag(&result);
} else
} else if (strcmp(token->content.string, "filter") == 0) {
if (strcmp(token->content.string, "print") == 0) {
compile_enter_tag(&result);
string_append(&result, "{{");
string_append(&result,compile_expression(token->next));
string_append(&result, "}}");
string_append(&result, "filter ");
string_append_free(&result,compile_expression(token->next));
compile_exit_tag(&result);
string_append_free(&result,compile(token->next->next));
compile_enter_tag(&result);
string_append(&result, "endfilter");
compile_exit_tag(&result);
} else if (strcmp(token->content.string, "print") == 0) {
for(tSyntaxElement* i = token->next; i != NULL; i = i->next) {
string_append(&result, "{{");
string_append_free(&result,compile_expression(i));
string_append(&result, "}}");
}
} else if (strcmp(token->content.string, "fn") == 0) {
compile_enter_tag(&result);
string_append(&result, "macro ");
string_append_free(&result, compile_expression(token->next));
compile_exit_tag(&result);
string_append_free(&result, compile(token->next->next));
compile_enter_tag(&result);
string_append(&result, "endmacro");
compile_exit_tag(&result);
} else if (strcmp(token->content.string, "transform") == 0) {
compile_enter_tag(&result);
string_append(&result, "transform ");
string_append_free(&result, compile_expression(token->next->content.syntax));
string_append(&result, " in ");
string_append_free(&result, compile_expression(token->next->content.syntax->next));
string_append(&result, " as ");
string_append_free(&result, compile_expression(token->next->content.syntax->next->next));
compile_exit_tag(&result);
string_append_free(&result, compile(token->next->next));
compile_enter_tag(&result);
string_append(&result, "endtransform");
compile_exit_tag(&result);
} else if (strcmp(token->content.string, "for") == 0) {
compile_enter_tag(&result);
string_append(&result, "for ");
string_append_free(&result, compile_expression(token->next->content.syntax));
string_append(&result, " in ");
string_append_free(&result, compile_expression(token->next->content.syntax->next));
compile_exit_tag(&result);
string_append_free(&result, compile(token->next->next));
compile_enter_tag(&result);
string_append(&result, "endfor");
compile_exit_tag(&result);
} else if (strcmp(token->content.string, "break") == 0) {
compile_enter_tag(&result);
string_append(&result, "break");
compile_exit_tag(&result);
} else if (strcmp(token->content.string, "continue") == 0) {
compile_enter_tag(&result);
string_append(&result, "continue");
compile_exit_tag(&result);
} else if (strcmp(token->content.string, "formap") == 0) {
compile_enter_tag(&result);
string_append(&result, "for ");
string_append_free(&result, compile_expression(token->next->content.syntax));
string_append(&result, ", ");
string_append_free(&result, compile_expression(token->next->content.syntax->next));
string_append(&result, " in ");
string_append_free(&result, compile_expression(token->next->content.syntax->next->next));
compile_exit_tag(&result);
string_append_free(&result, compile(token->next->next));
compile_enter_tag(&result);
string_append(&result, "endfor");
compile_exit_tag(&result);
} else if (strcmp(token->content.string, "verbatim") == 0) {
compile_enter_tag(&result);
string_append(&result, "verbatim");
compile_exit_tag(&result);
for(tSyntaxElement* i = token->next; i != NULL; i = i->next) {
string_append_free(&result,string_unescape(i->content.string));
}
compile_enter_tag(&result);
string_append(&result, " endverbatim");
compile_exit_tag(&result);
} else if (strcmp(token->content.string, "assert") == 0) {
compile_enter_tag(&result);
string_append(&result, "require ");
string_append_free(&result, compile_expression(token->next));
string_append(&result, " returning ");
string_append_free(&result, compile_expression(token->next->next));
compile_exit_tag(&result);
} else if (strcmp(token->content.string, "quote") == 0) {
if (token->next->type == STRING)
string_append(&result,token->next->content.string);
else
string_append_free(&result,compile_expression(token->next));
} else {
string_append(&result, "{#");
string_append(&result, "Unknown keyword: ");
string_append(&result, token->content.string);
string_append(&result, "#}");
}
+18 -1
View File
@@ -31,7 +31,24 @@ int main(int argc, char *argv[]) {
return 1;
}
printf("%s\n", compile(code));
#ifdef PROCESS_DEBUG
printf("Parsed: \n");
printtree(code, 0);
process(code, NULL);
printf("\nProcessed: \n");
printtree(code, 0);
#else
process(code, NULL);
char* compiled = compile(code);
printf("%s\n", compiled);
free(compiled);
compiled = NULL;
#endif
se_free(code);
code = NULL;
+29 -12
View File
@@ -10,9 +10,14 @@ bool replace(tSyntaxElement* tree, tProcessingData* pd) {
free(tree->content.none);
tSyntaxElement* clone = se_clone_no_next(pd->data.replace.replacewiththis, tree);
tSyntaxElement* clone = se_clone(pd->data.replace.replacewiththis, tree);
tree->content = clone->content;
tree->type = clone->type;
if (clone->next != NULL) {
tSyntaxElement* tmp = se_find_last(clone);
tmp->next = tree->next;
tree->next = clone->next;
}
free(clone);
@@ -21,9 +26,10 @@ bool replace(tSyntaxElement* tree, tProcessingData* pd) {
return false;
}
void process_mod(tSyntaxElement* tree, tProcessingData* pdata) {
bool process_mod(tSyntaxElement* tree, tProcessingData* pdata) {
tProcessingData* pd = pdata;
bool b = false;
while (1) {
@@ -33,7 +39,8 @@ void process_mod(tSyntaxElement* tree, tProcessingData* pdata) {
if (tree->type == TOKEN) {
switch (pd->type) {
case REPLACE:
replace(tree,pd);
if (replace(tree,pd))
b = true;
break;
case FUNCTION:
break;
@@ -47,6 +54,8 @@ void process_mod(tSyntaxElement* tree, tProcessingData* pdata) {
}
return b;
}
bool process_find(tSyntaxElement* tree, tProcessingData** p_pdata) {
@@ -61,7 +70,7 @@ bool process_find(tSyntaxElement* tree, tProcessingData** p_pdata) {
tSyntaxElement* token = tree->content.syntax;
if (strcmp(token->content.string,"def") == 0 &&
if (strcmp(token->content.string,"#def") == 0 &&
token->next != NULL &&
token->next->next != NULL) {
@@ -69,7 +78,7 @@ bool process_find(tSyntaxElement* tree, tProcessingData** p_pdata) {
tSyntaxElement* replacewiththis = token->next->next;
tProcessingData* npd = malloc(sizeof(tSyntaxElement));
tProcessingData* npd = malloc(sizeof(tProcessingData));
npd->type = REPLACE;
npd->data.replace.replacethis = replacethis;
@@ -80,12 +89,12 @@ bool process_find(tSyntaxElement* tree, tProcessingData** p_pdata) {
deffound = true;
} else if (strcmp(token->content.string,"fn") == 0 &&
} else if (strcmp(token->content.string,"#fn") == 0 &&
token->next != NULL && token->next->type == TOKEN &&
token->next->next != NULL && se_istraversable(token->next->next) &&
token->next->next->next != NULL) {
tProcessingData* npd = malloc(sizeof(tSyntaxElement));
tProcessingData* npd = malloc(sizeof(tProcessingData));
npd->type = FUNCTION;
@@ -124,11 +133,12 @@ bool process_find(tSyntaxElement* tree, tProcessingData** p_pdata) {
}
void process(tSyntaxElement* tree, tProcessingData* pdata) {
bool process(tSyntaxElement* tree, tProcessingData* pdata) {
tProcessingData* pd = pdata;
bool deffound = false;
bool didreplace = false;
tProcessingData* firstpd = NULL;
@@ -142,22 +152,29 @@ void process(tSyntaxElement* tree, tProcessingData* pdata) {
deffound = process_find(tree, &pd);
if (tree->next != NULL) {
process(tree->next, pd);
if (process(tree->next, pd))
didreplace = true;
}
if (!deffound) {
if (se_istraversable(tree))
process(tree->content.syntax, pd);
if (process(tree->content.syntax, pd))
didreplace = true;
process_mod(tree, pd);
if (process_mod(tree, pd))
didreplace = true;
}
if (pd != pdata)
free(pd);
if (firstpd != pd && firstpd != NULL)
if (firstpd != pd && firstpd != NULL) {
free(firstpd);
if (didreplace)
process(tree, NULL);
}
return didreplace;
}
+1 -1
View File
@@ -45,4 +45,4 @@ typedef struct ProcessingData {
void process(tSyntaxElement*, tProcessingData*);
bool process(tSyntaxElement*, tProcessingData*);
+11 -1
View File
@@ -102,7 +102,7 @@ tSyntaxElement* se_clone(tSyntaxElement* syntaxelement, tSyntaxElement* newtop)
clone = se_create();
clone->type = syntaxelement->type;
clone->content.string = malloc(strlen(syntaxelement->content.string));
clone->content.string = malloc(strlen(syntaxelement->content.string) + sizeof(char));
strcpy(clone->content.string, syntaxelement->content.string);
if (syntaxelement->next != NULL)
@@ -142,3 +142,13 @@ tSyntaxElement* se_clone_no_next(tSyntaxElement* syntaxelement, tSyntaxElement*
return clone;
}
tSyntaxElement* se_find_last(tSyntaxElement* syntaxelement) {
tSyntaxElement* i = syntaxelement;
while (1)
if (i->next == NULL)
return i;
else
i = i->next;
}
+2
View File
@@ -47,4 +47,6 @@ tSyntaxElement* se_clone(tSyntaxElement*, tSyntaxElement*);
tSyntaxElement* se_clone_no_next(tSyntaxElement*, tSyntaxElement*);
tSyntaxElement* se_find_last(tSyntaxElement*);
#endif