ocaml: phase 6 Array module + (op) operator sections (+6 tests, 512 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Array module (runtime.sx, OCaml syntax):
Backed by a 'ref of list'. make/length/get/init build the cell;
set rewrites the underlying list with one cell changed (O(n) but
works for short arrays in baseline programs). Includes
iter/iteri/map/mapi/fold_left/to_list/of_list/copy/blit/fill.
(op) operator sections (parser.sx, parse-atom):
When the token after '(' is a binop (any op with non-zero
precedence in the binop table) and the next token is ')', emit
(:fun ('a' 'b') (:op OP a b)) — i.e. (+) becomes fun a b -> a + b.
Recognises every binop including 'mod', 'land', '^', '@', '::',
etc.
Lets us write:
List.fold_left (+) 0 [1;2;3;4;5] = 15
let f = ( * ) in f 6 7 = 42
List.map ((-) 10) [1;2;3] = [9;8;7]
let a = Array.make 5 7 in
Array.set a 2 99;
Array.fold_left (+) 0 a = 127
This commit is contained in:
@@ -419,6 +419,23 @@
|
||||
(advance-tok!)
|
||||
(cond
|
||||
((at-op? ")") (begin (advance-tok!) (list :unit)))
|
||||
;; (op) — operator section: build (fun a b -> a op b).
|
||||
;; Recognises ops with precedence > 0 (i.e. binops),
|
||||
;; followed immediately by `)`.
|
||||
((and (or (= (ocaml-tok-type (peek-tok)) "op")
|
||||
(= (ocaml-tok-type (peek-tok)) "keyword"))
|
||||
(not (= (ocaml-binop-prec
|
||||
(ocaml-tok-value (peek-tok))) 0))
|
||||
(let ((t1 (nth tokens (+ idx 1))))
|
||||
(and (= (ocaml-tok-type t1) "op")
|
||||
(= (ocaml-tok-value t1) ")"))))
|
||||
(let ((opv (ocaml-tok-value (peek-tok))))
|
||||
(begin
|
||||
(advance-tok!)
|
||||
(advance-tok!)
|
||||
(list :fun (list "a" "b")
|
||||
(list :op opv (list :var "a")
|
||||
(list :var "b"))))))
|
||||
(else
|
||||
(let
|
||||
((e (parse-expr)))
|
||||
|
||||
Reference in New Issue
Block a user