aboutsummaryrefslogtreecommitdiff
path: root/rip.hs
blob: 6dddb20d6c3379b665d83f20ec11bfc559fa2606 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
module Main where

import System.Environment
import System.Exit
import Control.Exception
import Data.Char
import Data.Either

type Stackelem = Integer


c_ripmode :: Bool
c_ripmode = True


booltoint :: (Integral a) => Bool -> a
booltoint False = 0
booltoint True = 1

riperror :: String -> IO a
riperror s = do
	if c_ripmode
		then putStrLn "rip"
		else putStrLn $ "ERROR: " ++ s
	exitFailure

fromRight :: Either a b -> b
fromRight (Right r) = r

fromLeft :: Either a b -> a
fromLeft (Left l) = l

parenpairs :: String -> Either String [(Int,Int)]
parenpairs = go 0 []
	--       index  stack   string
	where go   _      []      []    = Right []
	      go   i      st   ('[':cs) = go (i+1) (i:st) cs
	      go   i    (s:st) (']':cs) = either Left (\x -> Right $ (s,i):x) $ go (i+1) st cs
	      go   i      []   (']':cs) = Left "rip -- unmatched closing bracket"
	      go   i      st    (_:cs)  = go (i+1) st cs
	      go   _    (_:_)     []    = Left "rip -- unmatched open bracket"

-- returns string in and after codeblock
getcodeblock :: String -> Either String (String,String)
getcodeblock s = if head (dropWhile isSpace s) == '[' then getcodeblock' s else Left "rip -- no codeblock when expected"

getcodeblock' :: String -> Either String (String,String)
getcodeblock' s = either Left (\_ -> Right (inblock,afterblock)) parenserr
	where
		afterwhite = dropWhile isSpace s
		parenserr = parenpairs afterwhite
		parens = fromRight parenserr
		mainpair = minimum parens
		blockend = snd mainpair
		inblock = drop 1 $ take blockend afterwhite
		afterblock = drop (blockend + 1) afterwhite

-- returns the resulting stack
-- rip: Rip InterPreter (recursive acronym)
rip :: String -> IO [Stackelem]
rip s = rip' s []

rip' :: String -> [Stackelem] -> IO [Stackelem]
rip' [] st = return st
rip' (x:xs) st = case x of
	n | '0' <= n && n <= '9' ->
		rip' xs (fromIntegral (ord n - ord '0') : st)

	'P' ->
		rip' xs (tail st)

	'S' ->
		rip' xs (b:a:cs)
			where (a:b:cs) = st

	'D' ->
		rip' xs (head st : st)

	'i' ->
		rip' xs (head st + 1 : tail st)

	'd' ->
		rip' xs (head st - 1 : tail st)

	'r' ->
		rip' xs res
			where
				n = head st
				newst = tail st
				(begin, rest) = (take (fromIntegral n) newst, drop (fromIntegral n) newst)
				res = tail begin ++ head begin : rest

	'R' ->
		rip' xs res
			where
				n = head st
				newst = tail st
				(begin, rest) = (take (fromIntegral n) newst, drop (fromIntegral n) newst)
				res = last begin : init begin ++ rest  --SLOW!

	'a' ->
		rip' xs (a + b : cs)
			where (b:a:cs) = st

	's' ->
		rip' xs (a - b : cs)
			where (b:a:cs) = st

	'm' ->
		rip' xs (a * b : cs)
			where (b:a:cs) = st

	'q' ->
		rip' xs (a `div` b : cs)
			where (b:a:cs) = st

	'G' ->
		rip' xs (booltoint (a > b) : cs)
			where (b:a:cs) = st

	'L' ->
		rip' xs (booltoint (a < b) : cs)
			where (b:a:cs) = st

	'E' ->
		rip' xs (booltoint (a == b) : cs)
			where (b:a:cs) = st

	'I' ->
		let maybecb = getcodeblock xs
		in case maybecb of
			Left s -> riperror s

			Right (inblock,afterblock) ->
				if head st /= 0
					then do
						newstack <- rip' inblock (tail st)
						rip' afterblock newstack
					else rip' afterblock (tail st)

	'W' ->
		let maybecb = getcodeblock xs
		in case maybecb of
			Left s -> riperror s

			Right (inblock,afterblock) ->
				let
					doloop st = do
						newstack <- rip' inblock st
						if head newstack /= 0
							then doloop $ tail newstack
							else return newstack
				in if head st /= 0
					then do
						newstack <- doloop (tail st)
						rip' afterblock newstack
					else rip' afterblock (tail st)

	'o' -> do
		putStr [chr (fromIntegral $ head st)]
		rip' xs (tail st)

	'O' -> do
		putStr $ show (head st)
		rip' xs (tail st)

	'g' -> do
		c <- getChar
		rip' xs (fromIntegral (ord c) : st)

	'$' -> do
		print st
		rip' xs st

	c | isSpace c ->
		rip' xs st

	_ -> riperror $ "Unknown command '" ++ [x,'\'']


main :: IO ()
main = do
	argv <- getArgs
	if length argv /= 1
		then do
			putStrLn "Pass a rip file as the command-line argument"
			exitFailure
		else do
			source <- readFile (argv !! 0)
			rip source
			return ()