Apple3.14/implementation.ijs
Jump to navigation
Jump to search
This is an implementation of Apple3.14 in J, written by User:Conor O'Brien.
Assumptions
I assume the following, since detail is to be desired on the actual spec.
- All programs must start with
apple3.14
in that case exactly, followed by an optional carriage return (\r
) and a necessary newline (\n
); - all programs must end with the
end
command, and that any amount of text may come after it, but will not be executed; - all lines not starting with
goto
followed by a space are ignored; - any invalid number passed to a
goto
command causes an error; - negative numbers passed to a
goto
command print an newline; - and that multiple
goto
s are allowed.
Apple314.ijs
Without further ado, this is the program, Apple314.ijs
:
getpi =: '3'"_`('3.',[:":10#.[:([:-/1 10*[:<.@o.10x^1 0+])"0 i.)@.* isn =: [:<./'0123456789'e.~] throw =: (5)1!:2&5,&LF 3 : 0]1!:1(3) NB. split on newlines and remove carriage returns lines =. <;._1 LF, y -. CR if. -. 'apple3.14' -: > {. lines do. throw 'error: first line is not "apple3.14"' end. NB. remove first line lines =. }. lines for_line. lines do. if. 'end' -: >line do. return. elseif. 'goto' -: > 0 { wln =. ;: >line do. num =. >1 { wln if. isn num do. echo getpi ". num elseif. ('-' -: {. num) *. isn }. num do. echo '' elseif. 1 do. throw 'error: malformed number "' , num , '"' end. end. end. ) exit 0
Running the program
You can execute programs like this:
On windows:
type <filename> | Apple314.ijs
On bash/related:
cat <filename> | Apple314.ijs
Further notes
This program currently does not display the entirety of sufficiently long strings. However, this is currently being worked on.