Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

One word

Name: Anonymous 2007-04-29 17:18 ID:mkdrvOsy

I am thinking of writing a parser/interpreter/compiler for a functional language implementing the Industry Standard of Forced Indentation of Code, this is what a sixliner in Haskell looks like:[see below], pretty neat, eh?

if
  and
    map
      y .
        ==
          mod
            x
            y
          0 
      filter
        x .
          /=
            mod
              20
              x 
            0
        1 .. 20
  x
  asd
    +
      x
      20

Name: Anonymous 2007-04-30 15:13 ID:Heaven

>>9
I just finished a simple evaluator as well -- next is ... lambdas perhaps, that'll probably be hard though.
data Expression = Value Int
                | Invocation String [Expression]

parseTree :: [String] -> Tree String
parseTree (x:xs) =
  Node x (map parseTree (findSubTrees (removeIndentation xs)))

removeIndentation [] = []
removeIndentation xs@(x:_) =
  map (drop (length (takeWhile (== ' ') x))) xs
   
findSubTrees [] = []
findSubTrees (x:xs) =
  let (a, b) = span (\ (y:_) -> y == ' ') xs
  in (x:a):(findSubTrees b)

parse = parseTree . lines

interpret :: Tree String -> Expression
interpret (Node n []) = (Value (read n))
interpret (Node n xs) = (Invocation n (map interpret xs))
 
intrinsic_plus (a:b:_) = a + b

intrinsics :: [(String, [Int] -> Int)]
intrinsics = [("+", intrinsic_plus)]

invoke :: String -> [Int] -> Int
invoke fn xs =
  case lookup fn intrinsics of
    Nothing -> error "No such intrinsic."
    Just f  -> f xs

evaluate (Value n) = n
evaluate (Invocation f xs) = invoke f (map evaluate xs)

main = readFile "test2.forced" >>= return . evaluate . interpret . parse

This is all it can do so far (I think it supports any indentation as long as it is the same on the same level):
+
 +
  +
   3
   4
  5
 6

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List