ocaml: phase 5.1 array literals [|...|] + lis.ml baseline (LIS = 6)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 35s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 35s
Added parser support for OCaml array literal syntax:
[| e1; e2; ...; en |] --> Array.of_list [e1; e2; ...; en]
[||] --> Array.of_list []
Desugaring keeps the array representation unchanged (ref-of-list)
since Array.of_list is a no-op constructor for that backing.
Tokenizer emits [, |, |, ] as separate ops; parser detects [ followed
by | and enters array-literal mode, terminating on |].
Baseline lis.ml exercises the syntax:
let lis arr =
let n = Array.length arr in
let dp = Array.make n 1 in
for i = 1 to n - 1 do
for j = 0 to i - 1 do
if arr.(j) < arr.(i) && dp.(j) + 1 > dp.(i) then
dp.(i) <- dp.(j) + 1
done
done;
let best = ref 0 in
for i = 0 to n - 1 do
if dp.(i) > !best then best := dp.(i)
done;
!best
lis [|10; 22; 9; 33; 21; 50; 41; 60; 80|] = 6
137 baseline programs total.
This commit is contained in:
@@ -68,6 +68,7 @@
|
||||
"fizz_classifier.ml": 540,
|
||||
"fizzbuzz.ml": 57,
|
||||
"flatten_tree.ml": 28,
|
||||
"lis.ml": 6,
|
||||
"list_ops.ml": 30,
|
||||
"luhn.ml": 2,
|
||||
"mat_mul.ml": 621,
|
||||
|
||||
18
lib/ocaml/baseline/lis.ml
Normal file
18
lib/ocaml/baseline/lis.ml
Normal file
@@ -0,0 +1,18 @@
|
||||
let lis arr =
|
||||
let n = Array.length arr in
|
||||
let dp = Array.make n 1 in
|
||||
for i = 1 to n - 1 do
|
||||
for j = 0 to i - 1 do
|
||||
if arr.(j) < arr.(i) && dp.(j) + 1 > dp.(i) then
|
||||
dp.(i) <- dp.(j) + 1
|
||||
done
|
||||
done;
|
||||
let best = ref 0 in
|
||||
for i = 0 to n - 1 do
|
||||
if dp.(i) > !best then best := dp.(i)
|
||||
done;
|
||||
!best
|
||||
|
||||
;;
|
||||
|
||||
lis [|10; 22; 9; 33; 21; 50; 41; 60; 80|]
|
||||
@@ -468,6 +468,47 @@
|
||||
(advance-tok!)
|
||||
(cond
|
||||
((at-op? "]") (begin (advance-tok!) (list :list)))
|
||||
;; Array literal `[| e1; e2; ...; en |]` desugars to
|
||||
;; `Array.of_list [e1; e2; ...; en]`. Empty `[||]`
|
||||
;; → `Array.of_list []`.
|
||||
((at-op? "|")
|
||||
(begin
|
||||
(advance-tok!)
|
||||
(cond
|
||||
((at-op? "|")
|
||||
(begin
|
||||
(advance-tok!)
|
||||
(consume! "op" "]")
|
||||
(list :app
|
||||
(list :field (list :con "Array")
|
||||
"of_list")
|
||||
(list :list))))
|
||||
(else
|
||||
(let
|
||||
((items (list)))
|
||||
(begin
|
||||
(append! items (parse-expr-no-seq))
|
||||
(define
|
||||
aloop
|
||||
(fn
|
||||
()
|
||||
(when
|
||||
(at-op? ";")
|
||||
(begin
|
||||
(advance-tok!)
|
||||
(when
|
||||
(not (at-op? "|"))
|
||||
(begin
|
||||
(append! items
|
||||
(parse-expr-no-seq))
|
||||
(aloop)))))))
|
||||
(aloop)
|
||||
(consume! "op" "|")
|
||||
(consume! "op" "]")
|
||||
(list :app
|
||||
(list :field (list :con "Array")
|
||||
"of_list")
|
||||
(cons :list items))))))))
|
||||
(else
|
||||
(let
|
||||
((items (list)))
|
||||
|
||||
Reference in New Issue
Block a user