Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 138 lines (129 sloc) 3.583 kb
abad53e @fperrad import following languages : bf, chitchat, forth, jako, lisp, ook, schem...
fperrad authored
1 # $Id$
2
3 =head1 NAME
4
5 ook.pasm - An implementation of ook in PASM
6
7 =cut
8
9 # First, read the file.
10 get_params "0", P5
11 set S20, P5[1] # Name of the Ook source.
12 open P20, S20, "r" # P20 = file descriptor
13 set S21, "" # S21 = accumulator
14 READ:
15 read S22, P20, 256
16 length I20, S22
17 le I20, 0, EOF
18 concat S21, S22
19 branch READ
20 EOF:
21 close P20
22
23 # Then, parse it to translate it.
24 length I20, S21 # Total length of file.
25 set I21, 0 # Char number in the file.
26 set I22, 1 # Line number (for error reporting).
27 new P21, 'ResizablePMCArray' # While-level.
28 push P21, 0
29 set S22, "" # Current char.
30 set S23, "" # Current instruction.
31 set S24, "\tnew P20, 'ResizablePMCArray'\n\tset I20,0\n" # Code generated.
32 branch LOOP_END
33 LOOP:
34 length I24, S23
35 eq I24, 8, LOOP_CHECK_INSTRUCTION
36 substr S22, S21, I21, 1
37 inc I21
38 eq S22, "\n", LOOP_LINEFEED
39 eq S22, "\t", LOOP_END
40 eq S22, " ", LOOP_END
41 concat S23, S22
42 branch LOOP_END
43
44 LOOP_CHECK_INSTRUCTION:
45 ne S23, "Ook.Ook?", LOOP_NOT_MOVER
46 concat S24, "\tinc I20\n"
47 set S23, ""
48 branch LOOP_END
49 LOOP_NOT_MOVER:
50 ne S23, "Ook?Ook.", LOOP_NOT_MOVEL
51 concat S24, "\tdec I20\n"
52 set S23, ""
53 branch LOOP_END
54 LOOP_NOT_MOVEL:
55 ne S23, "Ook.Ook.", LOOP_NOT_INC
56 concat S24, "\tset I21,P20[I20]\n\tinc I21\n\tset P20[I20],I21\n"
57 set S23, ""
58 branch LOOP_END
59 LOOP_NOT_INC:
60 ne S23, "Ook!Ook!", LOOP_NOT_DEC
61 concat S24, "\tset I21,P20[I20]\n\tdec I21\n\tset P20[I20],I21\n"
62 set S23, ""
63 branch LOOP_END
64 LOOP_NOT_DEC:
65 ne S23, "Ook!Ook?", LOOP_NOT_WHILE
66 bsr MAKE_LABEL
67 concat S24, "\tbranch OOK"
68 concat S24, S26
69 concat S24, "\nKOO"
70 concat S24, S26
71 concat S24, ":\n"
72 push P21, 0
73 set S23, ""
74 branch LOOP_END
75 LOOP_NOT_WHILE:
76 ne S23, "Ook?Ook!", LOOP_NOT_ELIHW
77 pop I25, P21
78 bsr MAKE_LABEL
79 concat S24, "OOK"
80 concat S24, S26
81 concat S24, ":\n\tset I21,P20[I20]\n\tne I21,0,KOO"
82 concat S24, S26
83 concat S24, "\n"
84 pop I27, P21
85 inc I27
86 push P21, I27
87 set S23, ""
88 branch LOOP_END
89 LOOP_NOT_ELIHW:
90 ne S23, "Ook!Ook.", LOOP_NOT_PRINT
91 concat S24, "\tset I21,P20[I20]\n\tchr S21,I21\n\tprint S21\n"
92 set S23, ""
93 branch LOOP_END
94 LOOP_NOT_PRINT:
95 ne S23, "Ook.Ook!", LOOP_NOT_READ
96 set S23, ""
97 branch LOOP_END
98 LOOP_NOT_READ:
99 print "OOK? "
100 print S20
101 print ":"
102 print I22
103 print " "
104 print S23
105 print "\n"
106 end
107 LOOP_LINEFEED:
108 inc I22
109 # Fallthru.
110 LOOP_END:
111 le I21, I20, LOOP
112 concat S24, "\tset_returns \"()\"\n"
113 concat S24, "\treturncc\n"
114
115 # Execute the generated code in S24
116 compreg P1, "PASM"
117 set_args "0", S24
118 get_results "0", P0
119 invokecc P1
120 invokecc P0
121 end
122
123 # Given the content of P21, create a label of integers concatenated in S26.
124 MAKE_LABEL:
125 set I25, P21
126 set I26, 0
127 set S26, ""
128 branch LABEL_END
129 LABEL_LOOP:
130 concat S26, "_"
131 set I27, P21[I26]
132 set S27, I27
133 concat S26, S27
134 inc I26
135 LABEL_END:
136 lt I26, I25, LABEL_LOOP
137 ret
Something went wrong with that request. Please try again.