Commit 5ef08c1d authored by Guy Watson's avatar Guy Watson
Browse files

Add for loop idiomatic generation. Not quite working yet :(

parent 26bb63fb
......@@ -94,6 +94,12 @@
[then : Expr])]
[Block Expr ([e : Expr])]
[Loop Expr ([e : Expr])]
[ForLoop Expr ([loopvar : GlobalSet] ;; This will be the inital value set before entering the loop
;; Rendering for this node is restricted: it will not produce
;; a value for use by children
[iter : GlobalSet]) ;; this iter is controlled by 'fresh', and will produce an
;; expression for the result of the loop
#:prop choice-weight 100]
[Branch Expr ([val : Expr]
[targetindex]) ;; a number depth
......@@ -159,6 +165,28 @@
#;[GlobalGet (hash 'name (fresh-var-name "$global_"))]
#;[GlobalSet (hash 'name (fresh-var-name "$global_"))]
;; implement these with the upcoming custom name changes
[ForLoop (let* ([global-name (ast-child 'name (ast-child 'loopvar (current-hole)))]
[global-get (make-fresh-node 'GlobalGet
(hash 'name global-name))]
[zero (make-fresh-node 'LiteralInt
(hash 'v 0))]
[one (make-fresh-node 'LiteralInt
(hash 'v 1))]
[iteration (make-fresh-node ('Subtraction
(hash 'l global-get
'r one)))]
[comparison (make-fresh-node 'LessThanOrEqualSigned
(hash 'l zero
'r global-get))]
[branch (make-fresh-node 'BranchIf
(hash 'cond comparison))])
(hash 'loopvar (make-fresh-node 'GlobalSet
(hash 'val (make-fresh-node 'LiteralInt)
'expr (make-fresh-node 'LiteralInt))) ;;make a dummy int
'iter (make-fresh-node 'GlobalSet
(hash 'val iteration
'name global-name
'expr branch))))]
......@@ -203,6 +231,8 @@
'then int))]]
[Block [int (λ (n t) (hash 'e int))]]
[Loop [int (λ (n t) (hash 'e int))]]
[ForLoop [int (λ (n t) (hash 'loopvar int
'e int))]]
[Branch [int (λ (n t) (hash 'val int))]]
[BranchIf [int (λ (n t) (hash 'cond int
'val int))]]
......@@ -234,6 +264,7 @@
[If (λ (n) #t)]
[Block (λ (n) #t)]
[Loop (λ (n) #t)]
[ForLoop (λ (n) #t)]
(define (get-nesting-types node)
......@@ -272,6 +303,8 @@
(not (eq? (ast-child 'cond n) c)))]
[If (λ (n c)
(not (eq? (ast-child 'cond n) c)))]
[ForLoop (λ (n c)
(not (eq? (ast-child 'loopvar n) c)))]
......@@ -363,6 +396,14 @@
'(loop (result i32))
(render-node (ast-child 'e n))
[ForLoop (λ (n)
(render-node (ast-child 'val (ast-child 'loopvar n)))
`(global.set ,(string->symbol (format "$~a" (ast-child 'name (ast-child 'loopvar n)))))
'(loop (result i32))
(render-node (ast-child 'iter n))
[Branch (λ (n)
(render-node (ast-child 'val n))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment