Skip to content

Commit 5469647

Browse files
gh-6: Add most THR-dependant operators and statements.
1 parent dd50145 commit 5469647

File tree

14 files changed

+1177
-119
lines changed

14 files changed

+1177
-119
lines changed

docs/SPECIFICATION.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -757,9 +757,9 @@
757757
758758
- `STOP(THR: thread):THR` — Cooperatively stop a running thread and mark it finished.
759759
760-
- `AWAIT(THR: thread):THR` — Block until the thread is finished.
760+
- `AWAIT(THR: thread):THR` — Block until `thread` is finished.
761761
762-
- `PAUSE(THR: thread, FLT: seconds=-1):THR` — Pause execution while preserving the current location. If `seconds` is provided and is $\ge 0$, the thread is automatically resumed after that duration. Pausing an already-paused thread is a runtime error.
762+
- `PAUSE(THR: thread, FLT: seconds=-1):THR` — Pause execution while preserving the current location. If `seconds` is provided and is >= 0, `thread` is automatically resumed after that duration. Pausing an already-paused thread is a runtime error.
763763
764764
- `RESUME(THR: thread):THR` — Resume a paused thread. Resuming a thread that is not paused is a runtime error.
765765

src/ast.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,15 @@ Expr* expr_wildcard(int line, int column) {
122122
return expr;
123123
}
124124

125+
Expr* expr_async(Stmt* block, int line, int column) {
126+
Expr* expr = ast_alloc(sizeof(Expr));
127+
expr->type = EXPR_ASYNC;
128+
expr->line = line;
129+
expr->column = column;
130+
expr->as.async.block = block;
131+
return expr;
132+
}
133+
125134
void expr_list_add(ExprList* list, Expr* expr) {
126135
if (list->count + 1 > list->capacity) {
127136
size_t new_cap = list->capacity == 0 ? 4 : list->capacity * 2;
@@ -155,6 +164,15 @@ Stmt* stmt_block(int line, int column) {
155164
return stmt;
156165
}
157166

167+
Stmt* stmt_async(Stmt* body, int line, int column) {
168+
Stmt* stmt = ast_alloc(sizeof(Stmt));
169+
stmt->type = STMT_ASYNC;
170+
stmt->line = line;
171+
stmt->column = column;
172+
stmt->as.async_stmt.body = body;
173+
return stmt;
174+
}
175+
158176
Stmt* stmt_expr(Expr* expr, int line, int column) {
159177
Stmt* stmt = ast_alloc(sizeof(Stmt));
160178
stmt->type = STMT_EXPR;
@@ -218,6 +236,17 @@ Stmt* stmt_for(char* counter, Expr* target, Stmt* body, int line, int column) {
218236
return stmt;
219237
}
220238

239+
Stmt* stmt_parfor(char* counter, Expr* target, Stmt* body, int line, int column) {
240+
Stmt* stmt = ast_alloc(sizeof(Stmt));
241+
stmt->type = STMT_PARFOR;
242+
stmt->line = line;
243+
stmt->column = column;
244+
stmt->as.parfor_stmt.counter = counter;
245+
stmt->as.parfor_stmt.target = target;
246+
stmt->as.parfor_stmt.body = body;
247+
return stmt;
248+
}
249+
221250
Stmt* stmt_func(char* name, DeclType ret, Stmt* body, int line, int column) {
222251
Stmt* stmt = ast_alloc(sizeof(Stmt));
223252
stmt->type = STMT_FUNC;
@@ -346,6 +375,9 @@ static void free_stmt_list(StmtList* list) {
346375
void free_expr(Expr* expr) {
347376
if (!expr) return;
348377
switch (expr->type) {
378+
case EXPR_ASYNC:
379+
free_stmt(expr->as.async.block);
380+
break;
349381
case EXPR_STR:
350382
free(expr->as.str_value);
351383
break;
@@ -391,6 +423,9 @@ void free_stmt(Stmt* stmt) {
391423
case STMT_BLOCK:
392424
free_stmt_list(&stmt->as.block);
393425
break;
426+
case STMT_ASYNC:
427+
free_stmt(stmt->as.async_stmt.body);
428+
break;
394429
case STMT_EXPR:
395430
free_expr(stmt->as.expr_stmt.expr);
396431
break;
@@ -418,6 +453,11 @@ void free_stmt(Stmt* stmt) {
418453
free_expr(stmt->as.for_stmt.target);
419454
free_stmt(stmt->as.for_stmt.body);
420455
break;
456+
case STMT_PARFOR:
457+
free(stmt->as.parfor_stmt.counter);
458+
free_expr(stmt->as.parfor_stmt.target);
459+
free_stmt(stmt->as.parfor_stmt.body);
460+
break;
421461
case STMT_FUNC:
422462
free(stmt->as.func_stmt.name);
423463
for (size_t i = 0; i < stmt->as.func_stmt.params.count; i++) {

src/ast.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ typedef enum {
2323
EXPR_PTR,
2424
EXPR_IDENT,
2525
EXPR_CALL,
26+
EXPR_ASYNC,
2627
EXPR_TNS,
2728
EXPR_MAP,
2829
EXPR_INDEX,
@@ -46,6 +47,7 @@ struct Expr {
4647
char* str_value;
4748
char* ident;
4849
char* ptr_name;
50+
struct { Stmt* block; } async;
4951
struct {
5052
Expr* callee;
5153
ExprList args;
@@ -72,12 +74,14 @@ struct Expr {
7274

7375
typedef enum {
7476
STMT_BLOCK,
77+
STMT_ASYNC,
7578
STMT_EXPR,
7679
STMT_ASSIGN,
7780
STMT_DECL,
7881
STMT_IF,
7982
STMT_WHILE,
8083
STMT_FOR,
84+
STMT_PARFOR,
8185
STMT_FUNC,
8286
STMT_RETURN,
8387
STMT_BREAK,
@@ -125,9 +129,11 @@ struct Stmt {
125129
} if_stmt;
126130
struct { Expr* condition; Stmt* body; } while_stmt;
127131
struct { char* counter; Expr* target; Stmt* body; } for_stmt;
132+
struct { char* counter; Expr* target; Stmt* body; } parfor_stmt;
128133
struct { char* name; ParamList params; DeclType return_type; Stmt* body; } func_stmt;
129134
struct { Expr* value; } return_stmt;
130135
struct { Expr* value; } break_stmt;
136+
struct { Stmt* body; } async_stmt;
131137
struct { char* name; Stmt* body; } thr_stmt;
132138
struct { Stmt* try_block; char* catch_name; Stmt* catch_block; } try_stmt;
133139
struct { Expr* target; } goto_stmt;
@@ -144,19 +150,22 @@ Expr* expr_ident(char* name, int line, int column);
144150
Expr* expr_call(Expr* callee, int line, int column);
145151
void call_kw_add(Expr* call, char* name, Expr* value);
146152
Expr* expr_tns(int line, int column);
153+
Expr* expr_async(Stmt* block, int line, int column);
147154
Expr* expr_map(int line, int column);
148155
Expr* expr_index(Expr* target, int line, int column);
149156
Expr* expr_range(Expr* start, Expr* end, int line, int column);
150157
Expr* expr_wildcard(int line, int column);
151158
void expr_list_add(ExprList* list, Expr* expr);
152159

153160
Stmt* stmt_block(int line, int column);
161+
Stmt* stmt_async(Stmt* body, int line, int column);
154162
Stmt* stmt_expr(Expr* expr, int line, int column);
155163
Stmt* stmt_assign(bool has_type, DeclType decl_type, char* name, Expr* target, Expr* value, int line, int column);
156164
Stmt* stmt_decl(DeclType decl_type, char* name, int line, int column);
157165
Stmt* stmt_if(Expr* cond, Stmt* then_branch, int line, int column);
158166
Stmt* stmt_while(Expr* cond, Stmt* body, int line, int column);
159167
Stmt* stmt_for(char* counter, Expr* target, Stmt* body, int line, int column);
168+
Stmt* stmt_parfor(char* counter, Expr* target, Stmt* body, int line, int column);
160169
Stmt* stmt_func(char* name, DeclType ret, Stmt* body, int line, int column);
161170
Stmt* stmt_return(Expr* value, int line, int column);
162171
Stmt* stmt_pop(char* name, int line, int column);

0 commit comments

Comments
 (0)