Skip to content

Commit a855404

Browse files
gh-5: Add MAP operators.
1 parent 7a2e267 commit a855404

File tree

13 files changed

+639
-26
lines changed

13 files changed

+639
-26
lines changed

docs/SPECIFICATION.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@
202202
203203
All other tensor operations are non-mutating: tensor literals and tensor-valued built-ins produce new tensor values rather than mutating existing ones. Because indexed assignment mutates a tensor object, if the same tensor value is aliased (bound to multiple identifiers, passed as an argument, or stored inside another tensor), all aliases observe the mutation.
204204
205+
MAP and `TNS` values are reference (aliasing) types: assigning a `MAP` or `TNS` to another identifier copies a reference to the same underlying container rather than performing an implicit deep copy. Mutating a map (via `map<...> = ...`, `DEL`, or other mutating operators) or mutating a tensor element through indexed assignment will be observed through any other identifier that references the same container. Use the built-in `COPY` (shallow copy) or `DEEPCOPY` (recursive deep copy) operators when a non-aliased duplicate is required.
206+
205207
Every runtime value has a static type: `INT`, `FLT`, `STR`, `TNS`, or `FUNC`. Integers are conceptually unbounded mathematical integers. Floats are IEEE754 binary floating-point numbers. Strings are sequences of characters (source text is ASCII, but escape codes may denote non-ASCII code points). Tensors are non-scalar aggregates whose elements may be `INT`, `FLT`, `STR`, `FUNC`, or `TNS`.
206208
207209
Function value (`FUNC`): a reference to a user-defined function body (including its lexical closure). A `FUNC` value can be stored in variables or tensors, passed as an argument, or returned from a function. The call syntax applies to any expression that evaluates to `FUNC`; for example, `alias()` calls the function bound to `alias`, and `tns[1]()` calls the function stored in that tensor element. `FUNC` values are always truthy; equality compares object identity (two references are equal only if they refer to the same function definition). String rendering produces an implementation-defined placeholder such as `<func name>`.

src/ast.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,15 @@ Stmt* stmt_expr(Expr* expr, int line, int column) {
143143
return stmt;
144144
}
145145

146-
Stmt* stmt_assign(bool has_type, DeclType decl_type, char* name, Expr* value, int line, int column) {
146+
Stmt* stmt_assign(bool has_type, DeclType decl_type, char* name, Expr* target, Expr* value, int line, int column) {
147147
Stmt* stmt = ast_alloc(sizeof(Stmt));
148148
stmt->type = STMT_ASSIGN;
149149
stmt->line = line;
150150
stmt->column = column;
151151
stmt->as.assign.has_type = has_type;
152152
stmt->as.assign.decl_type = decl_type;
153153
stmt->as.assign.name = name;
154+
stmt->as.assign.target = target;
154155
stmt->as.assign.value = value;
155156
return stmt;
156157
}
@@ -346,7 +347,8 @@ void free_stmt(Stmt* stmt) {
346347
free_expr(stmt->as.expr_stmt.expr);
347348
break;
348349
case STMT_ASSIGN:
349-
free(stmt->as.assign.name);
350+
if (stmt->as.assign.name) free(stmt->as.assign.name);
351+
if (stmt->as.assign.target) free_expr(stmt->as.assign.target);
350352
free_expr(stmt->as.assign.value);
351353
break;
352354
case STMT_DECL:

src/ast.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ struct Stmt {
105105
union {
106106
StmtList block;
107107
struct { Expr* expr; } expr_stmt;
108-
struct { bool has_type; DeclType decl_type; char* name; Expr* value; } assign;
108+
struct { bool has_type; DeclType decl_type; char* name; Expr* target; Expr* value; } assign;
109109
struct { DeclType decl_type; char* name; } decl;
110110
struct {
111111
Expr* condition;
@@ -139,7 +139,7 @@ void expr_list_add(ExprList* list, Expr* expr);
139139

140140
Stmt* stmt_block(int line, int column);
141141
Stmt* stmt_expr(Expr* expr, int line, int column);
142-
Stmt* stmt_assign(bool has_type, DeclType decl_type, char* name, Expr* value, int line, int column);
142+
Stmt* stmt_assign(bool has_type, DeclType decl_type, char* name, Expr* target, Expr* value, int line, int column);
143143
Stmt* stmt_decl(DeclType decl_type, char* name, int line, int column);
144144
Stmt* stmt_if(Expr* cond, Stmt* then_branch, int line, int column);
145145
Stmt* stmt_while(Expr* cond, Stmt* body, int line, int column);

0 commit comments

Comments
 (0)