aboutsummaryrefslogtreecommitdiff
path: root/src/ast.c
diff options
context:
space:
mode:
authorShinya Sato <shinnya.sato@gmail.com>2026-03-15 18:20:59 +0900
committerGitHub <noreply@github.com>2026-03-15 18:20:59 +0900
commit770d0378e42c947dbecb3d7e294ea433ac1f43d7 (patch)
tree0226352b3495d7318dd7295d3c6152ae2e489aa8 /src/ast.c
parent28703bd92c53e28ed020c2c24820a14c1c468c57 (diff)
parent4caf4984763dd50d2bcc2f8127296df6587ea891 (diff)
downloadinpla-770d0378e42c947dbecb3d7e294ea433ac1f43d7.tar.gz
inpla-770d0378e42c947dbecb3d7e294ea433ac1f43d7.zip
Merge pull request #4 from Lamagraph/list-parsing
Improve list parsing
Diffstat (limited to 'src/ast.c')
-rw-r--r--src/ast.c37
1 files changed, 31 insertions, 6 deletions
diff --git a/src/ast.c b/src/ast.c
index e776764..e1c6d9a 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -208,16 +208,41 @@ Ast *ast_paramToCons(Ast *ast) {
if (ast == NULL) {
return ast_makeAST(AST_NIL, NULL, NULL);
}
-
- Ast *head = ast->left;
- Ast *tail = ast->right;
- Ast *ret = ast_makeAST(AST_OPCONS, NULL,
- ast_makeList2(head, ast_paramToCons(tail)));
- return ret;
+ Ast *head = NULL, *tail = NULL, *current = ast;
+ while (current != NULL) {
+ head = current->left;
+ tail = current->right;
+ current->id = AST_OPCONS;
+ current->left = NULL;
+ if (tail == NULL) {
+ current->right = ast_makeList2(head, ast_makeAST(AST_NIL, NULL, NULL));
+ break;
+ }
+ else {
+ current->right = ast_makeList2(head, tail);
+ current = tail;
+ }
+ }
+ return ast;
}
+Ast *ast_reverseList(Ast *l) {
+ Ast *prev = NULL;
+ Ast *current = l;
+ Ast *next = NULL;
+ while (current != NULL) {
+ next = current->right;
+ current->right = prev;
+ prev = current;
+ current = next;
+ }
+ return prev;
+}
+Ast *ast_addFirst(Ast *l, Ast *p) {
+ return ast_makeAST(AST_LIST, p, l);
+}
Ast *ast_addLast(Ast *l, Ast *p)
{