Skip to content

Commit 1152aa2

Browse files
gh-9, gh-10: Add built-in tests to est.asmln, add DELETEFILE, fix doc\SPECIFICATION.html.
Fix minor error in SCAT.
1 parent c195ebd commit 1152aa2

File tree

4 files changed

+585
-16
lines changed

4 files changed

+585
-16
lines changed

asm-lang.exe

995 Bytes
Binary file not shown.

docs/SPECIFICATION.html

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -558,31 +558,31 @@
558558

559559
- `IN(ANY: value, TNS: tensor):INT` ; returns `1` if `value` is equal to any element of `tensor` (using the language's equality semantics), otherwise `0`.
560560

561-
### 12.2 Arithmetic (`INT` only)
561+
### 12.2 Arithmetic (`INT` or `FLT`, no mixing)
562562

563-
- `ADD(INT: a, INT: b):INT` ; a + b
563+
- `ADD(INT|FLT: a, INT|FLT: b):INT|FLT` ; a + b
564564

565-
- `SUB(INT: a, INT: b):INT` ; a - b
565+
- `SUB(INT|FLT: a, INT|FLT: b):INT|FLT` ; a - b
566566

567-
- `MUL(INT:a, INT:b):INT` ; a * b
567+
- `MUL(INT|FLT: a, INT|FLT: b):INT|FLT` ; a * b
568568

569-
- `DIV(INT: a, INT: b):INT` ; floor(a / b)
569+
- `DIV(INT|FLT: a, INT|FLT: b):INT|FLT` ; floor(a / b)
570570

571-
- `CDIV(INT: a, INT: b):INT` ; ceil(a / b)
571+
- `CDIV(INT|FLT: a, INT|FLT: b):INT|FLT` ; ceil(a / b)
572572

573-
- `POW(INT: a, INT: b):INT` ; a ^ b
573+
- `POW(INT|FLT: a, INT|FLT: b):INT|FLT` ; a ^ b
574574

575575
- `ROOT(INT|FLT: x, INT|FLT: n):INT|FLT` ; nth root of `x`. No mixing of `INT` and `FLT` is allowed. For `INT` arguments `n` must be non-zero; positive `n` returns the integer nth root (largest integer r with r^n <= x for x >= 0); negative `n` yields an integer result only for `x` equal to `1` or `-1` (reciprocal is integer), and `x < 0` requires odd `n`. For `FLT` arguments the result is `x^(1/n)` (negative `n` allowed); negative `x` is allowed only when `n` is an odd integer. Division by zero is an error.
576576
577-
- `MOD(INT: a, INT: b):INT` ; remainder of a / b
577+
- `MOD(INT|FLT: a, INT|FLT: b):INT|FLT` ; remainder of a / b
578578
579-
- `NEG(INT: a):INT` ; -a (additive inverse)
579+
- `NEG(INT|FLT: a):INT|FLT` ; -a (additive inverse)
580580
581-
- `ABS(INT: a):INT` ; absolute value of a
581+
- `ABS(INT|FLT: a):INT|FLT` ; absolute value of a
582582
583-
- `GCD(INT: a, INT: b):INT` ; greatest common divisor of a and b
583+
- `GCD(INT|FLT: a, INT|FLT: b):INT|FLT` ; greatest common divisor of a and b
584584
585-
- `LCM(INT: a, INT: b):INT` ; least common multiple of a and b
585+
- `LCM(INT|FLT: a, INT|FLT: b):INT|FLT` ; least common multiple of a and b
586586
587587
### 12.3 Bitwise / Boolean
588588
@@ -779,6 +779,8 @@
779779

780780
- `EXISTFILE(STR: path):INT` — Returns `INT` 1 when a filesystem object exists at `path`, otherwise returns `INT` 0. The argument must be a `STR`.
781781

782+
- `DELETEFILE(STR: path):INT` — Deletes the filesystem object at `path`. If no filesystem object exists at `path`, the interpreter raises a runtime error (rewrite: `DELETEFILE`). Permission errors and other filesystem failures also raise a runtime error (rewrite: `DELETEFILE`). On success `DELETEFILE` returns `INT` 1.
783+
782784
### 12.14 Symbol Freezing:
783785

784786
- `FREEZE(SYMBOL: symbol):INT` — Marks the identifier `symbol` as frozen. The first argument must be an identifier (not a string literal). On success `FREEZE` returns `INT` 0. Once a binding is frozen, any attempt to reassign it (via `=` assignment) or to delete it (`DEL`) raises a runtime error (assign attempts signal rewrite: `ASSIGN`; deletion attempts signal rewrite: `DEL`). `FREEZE` locates the binding in the usual lexical environment chain and freezes that specific binding; if the identifier is undefined, the call raises a runtime error (rewrite: `FREEZE`).

interpreter.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@ def __init__(self) -> None:
549549
self._register_custom("READFILE", 1, 2, self._readfile)
550550
self._register_custom("BYTES", 1, 2, self._bytes)
551551
self._register_custom("WRITEFILE", 2, 3, self._writefile)
552+
self._register_custom("DELETEFILE", 1, 1, self._deletefile)
552553
self._register_custom("EXISTFILE", 1, 1, self._existfile)
553554
self._register_custom("CL", 1, 1, self._cl)
554555
self._register_custom("EXIT", 0, 1, self._exit)
@@ -2865,6 +2866,26 @@ def _existfile(
28652866
path = self._expect_str(args[0], "EXISTFILE", location)
28662867
return Value(TYPE_INT, 1 if os.path.exists(path) else 0)
28672868

2869+
def _deletefile(
2870+
self,
2871+
interpreter: "Interpreter",
2872+
args: List[Value],
2873+
__: List[Expression],
2874+
___: Environment,
2875+
location: SourceLocation,
2876+
) -> Value:
2877+
path = self._expect_str(args[0], "DELETEFILE", location)
2878+
# If the path does not exist, signal an explicit runtime error.
2879+
if not os.path.exists(path):
2880+
raise ASMRuntimeError(f"DELETEFILE: '{path}' does not exist", location=location, rewrite_rule="DELETEFILE")
2881+
try:
2882+
# Attempt to remove the filesystem object. Use os.remove to
2883+
# fail on directories; caller must ensure target is a file.
2884+
os.remove(path)
2885+
except OSError as exc:
2886+
raise ASMRuntimeError(f"Failed to delete '{path}': {exc}", location=location, rewrite_rule="DELETEFILE")
2887+
return Value(TYPE_INT, 1)
2888+
28682889
# Tensor helpers
28692890
def _shape_from_tensor(self, tensor: Tensor, rule: str, location: SourceLocation) -> List[int]:
28702891
if len(tensor.shape) != 1:
@@ -3384,8 +3405,8 @@ def _scatter(
33843405
lo_raw = self._expect_int(pairs[axis, 0], "SCAT", location)
33853406
hi_raw = self._expect_int(pairs[axis, 1], "SCAT", location)
33863407
dim_len = dst.shape[axis]
3387-
lo = self._resolve_tensor_index(lo_raw, dim_len, "SCAT", location)
3388-
hi = self._resolve_tensor_index(hi_raw, dim_len, "SCAT", location)
3408+
lo = interpreter._resolve_tensor_index(lo_raw, dim_len, "SCAT", location)
3409+
hi = interpreter._resolve_tensor_index(hi_raw, dim_len, "SCAT", location)
33893410
if lo > hi:
33903411
raise ASMRuntimeError("SCAT expects lo <= hi for each dimension", location=location, rewrite_rule="SCAT")
33913412
span = hi - lo + 1

0 commit comments

Comments
 (0)