Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

2 parse text to ast #8

Merged
merged 21 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@

module Main (main) where

import AST (showMaybeTree)
import TextToAST (textToAST)

main :: IO ()
main = putStrLn "Hello, World!"
main = putStrLn (showMaybeTree (textToAST "(define javascriptIsGood #f)"))
20 changes: 17 additions & 3 deletions src/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,28 @@
module AST
(
Symbol,
Atom (Number, Symbol),
Atom (Number, Symbol, Boolean),
Tree (Node, Leaf),
showMaybeTree
) where

import Data.Int (Int64)

type Symbol = String

data Atom = Number Int64 | Symbol Symbol
data Atom = Number Int64 | Symbol Symbol | Boolean Bool

data Tree = Node Symbol [Tree] | Leaf Atom
data Tree = Node Symbol (Maybe Tree) (Maybe Tree) | Leaf Atom

showMaybeTree :: Maybe Tree -> String
showMaybeTree Nothing = "Nothing"
showMaybeTree (Just tree) = show tree

instance Show Atom where
show (Number value) = "Number : " ++ show value
show (Symbol value) = "Symbol : " ++ value
show (Boolean value) = "Boolean : " ++ show value

instance Show Tree where
show (Node value left right) = "Node : \"" ++ value ++ "\" left : \"" ++ showMaybeTree left ++ "\" right : \"" ++ showMaybeTree right ++ "\""
show (Leaf value) = "Leaf : " ++ show value
52 changes: 52 additions & 0 deletions src/TextToAST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,56 @@

module TextToAST
(
textToAST
) where

import AST
import Data.Int (Int64)
import Data.Char (isDigit)

isFunction :: String -> Bool
isFunction [] = False
isFunction (x:_) | x == '(' = True
Saverio976 marked this conversation as resolved.
Show resolved Hide resolved
| otherwise = False

skipableChar :: Char -> Bool
skipableChar x = x == ' ' || x == '\t'
Saverio976 marked this conversation as resolved.
Show resolved Hide resolved
Saverio976 marked this conversation as resolved.
Show resolved Hide resolved

notSkipableChar :: Char -> Bool
notSkipableChar x = not (skipableChar x)

stringIsNumber :: String -> Bool
stringIsNumber [] = False
stringIsNumber (x:[]) | Data.Char.isDigit x = True
guillaumeAbel marked this conversation as resolved.
Show resolved Hide resolved
stringIsNumber (x:xs) | Data.Char.isDigit x = stringIsNumber xs
| otherwise = False

nextToParse :: String -> String
nextToParse [] = []
nextToParse (x:xs) | skipableChar x = nextToParse xs
guillaumeAbel marked this conversation as resolved.
Show resolved Hide resolved
nextToParse str = dropWhile notSkipableChar str

createNode :: Symbol -> String -> Maybe Tree
createNode [] _ = Nothing
createNode _ [] = Nothing
createNode (_:xs) str = Just (Node xs (textToAST str) (textToAST (nextToParse str)))
guillaumeAbel marked this conversation as resolved.
Show resolved Hide resolved

stringIsBool :: String -> Bool
stringIsBool str = str == "#t" || str == "#f"
guillaumeAbel marked this conversation as resolved.
Show resolved Hide resolved

createBool :: String -> Maybe Tree
createBool str | str == "#t" = Just (Leaf (Boolean True))
| str == "#f" = Just (Leaf (Boolean False))
| otherwise = Nothing
guillaumeAbel marked this conversation as resolved.
Show resolved Hide resolved

treeFromAtom :: String -> String -> Maybe Tree
treeFromAtom [] _ = Nothing
treeFromAtom split _ | stringIsNumber split = Just (Leaf (AST.Number (read split :: Data.Int.Int64)))
| stringIsBool split = createBool split
treeFromAtom split str | isFunction split = createNode split (init str)
| otherwise = Just (Leaf (Symbol split))

textToAST :: String -> Maybe Tree
textToAST [] = Nothing
textToAST (x:xs) | skipableChar x = textToAST xs
textToAST str = treeFromAtom (takeWhile notSkipableChar str) (dropWhile notSkipableChar str)