diff --git a/README.markdown b/README.markdown index 8339974..9cb0926 100644 --- a/README.markdown +++ b/README.markdown @@ -119,3 +119,4 @@ TODO * insist on EOL after each instruction. need spacesWOEOL production * asl .a * `outputs` on externals +* Routine is a kind of StorageLocation? (Location)? diff --git a/doc/Emitting.markdown b/doc/Emitting.markdown index e8c7561..ce9a052 100644 --- a/doc/Emitting.markdown +++ b/doc/Emitting.markdown @@ -19,7 +19,6 @@ Emitting an `if`. | } | sta screen | } - = jmp main = main: = lda screen = cmp screen @@ -47,7 +46,6 @@ Emitting a `repeat`. | } | sty screen | } - = jmp main = main: = ldy zero = @@ -79,7 +77,6 @@ Nested ifs. | lda #3 | } | } - = jmp main = main: = BEQ _label_3 = lda #3 @@ -116,7 +113,6 @@ Installing an interrupt handler (at the Kernal level, i.e. with CINV) | inc screen | jmp (save_cinv) | } - = jmp main = main: = sei = lda cinv @@ -145,7 +141,6 @@ Copy command: immediate -> byte | routine main { | copy #23 position | } - = jmp main = main: = lda #23 = sta position @@ -159,7 +154,6 @@ Copy command: immediate -> word | routine main { | copy #$0400 position | } - = jmp main = main: = lda #0 = sta position @@ -168,3 +162,24 @@ Copy command: immediate -> word = rts = = position: .word 0 + +`main` is always emitted first. + + | reserve word position + | routine foo { + | inx + | } + | routine main { + | jsr foo + | jsr foo + | } + = main: + = jsr foo + = jsr foo + = rts + = + = foo: + = inx + = rts + = + = position: .word 0 diff --git a/doc/Instruction_Support.markdown b/doc/Instruction_Support.markdown index 1b58ee2..289fb3b 100644 --- a/doc/Instruction_Support.markdown +++ b/doc/Instruction_Support.markdown @@ -203,7 +203,6 @@ Big test for parsing and emitting instructions. | ora #8 | ora vbyte | } - = jmp main = main: = lda #4 = ldx #0 @@ -277,7 +276,6 @@ Big test for parsing and emitting instructions. | eor #5 | eor vbyte | } - = jmp main = main: = asl = asl vbyte @@ -307,7 +305,6 @@ Big test for parsing and emitting instructions. | lda #2 | } | } - = jmp main = main: = pha = sei diff --git a/src/SixtyPical/Emitter.hs b/src/SixtyPical/Emitter.hs index 5b97a97..f02007d 100644 --- a/src/SixtyPical/Emitter.hs +++ b/src/SixtyPical/Emitter.hs @@ -7,9 +7,13 @@ import Data.Bits import SixtyPical.Model emitProgram p@(Program decls routines) = - " jmp main\n" ++ - emitRoutines p routines ++ - emitDecls p decls + let + mains = findRoutines (\(Routine name _ _) -> name == "main") routines + allElse = findRoutines (\(Routine name _ _) -> name /= "main") routines + in + emitRoutines p mains ++ + emitRoutines p allElse ++ + emitDecls p decls emitDecls _ [] = "" emitDecls p (decl:decls) = diff --git a/src/SixtyPical/Model.hs b/src/SixtyPical/Model.hs index cd4cd66..a43985d 100644 --- a/src/SixtyPical/Model.hs +++ b/src/SixtyPical/Model.hs @@ -176,3 +176,8 @@ lookupRoutine' [] _ = Nothing lookupRoutine' (rout@(Routine rname _ _):routs) name | rname == name = Just rout | otherwise = lookupRoutine' routs name + +findRoutines f [] = [] +findRoutines f (rout:routs) + | f rout = (rout:findRoutines f routs) + | otherwise = findRoutines f routs