-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from nLatt/16-cpt-and-ast-implementation
16 cpt and ast implementation
- Loading branch information
Showing
32 changed files
with
286 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,4 @@ cabal.project.local | |
cabal.project.local~ | ||
.HTF/ | ||
.ghc.environment.* | ||
*.tix |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(+ (* 2 3) (div 10 2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(eq? (* 2 5) (- 11 1)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(< 1 (mod 10 3)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(div 10 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(* foo 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
(define (fact x) | ||
(if (eq? x 1) | ||
1 | ||
(* x (fact (- x 1))))) | ||
(fact 10) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
(define foo 21) | ||
(* foo 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
(define (add a b) | ||
(+ a b)) | ||
(add 3 4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(if #t 1 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(if #f 1 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
(define foo 42) | ||
(if (< foo 10) | ||
(* foo 3) | ||
(div foo 2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(lambda (a b) (+ a b)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
((lambda (a b) (+ a b)) 1 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
(define add | ||
(lambda (a b) | ||
(+ a b))) | ||
(add 3 4) |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
(define (> a b) | ||
(if (eq? a b) | ||
#f | ||
(if (< a b) | ||
#f | ||
#t))) | ||
(> 10 -2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
cradle: | ||
cabal: | ||
- path: "./parsing" | ||
- path: "./lib" | ||
component: "lib:GLaDOS2023" | ||
|
||
- path: "./app/Main.hs" | ||
|
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
module Parsing.Ast where | ||
|
||
import Parsing.Cpt | ||
|
||
-- ─── Abstract Syntaxe Tree ─────────────────────────────────────────────────────────────────────── | ||
|
||
data Expr = ExprList [Expr] | ||
| Num Integer | ||
| Symbole String | ||
| Call String [Expr] -- Will also be used for the boolean expression | ||
deriving (Eq, Show) | ||
|
||
-- This function parses lists of expressions, ignoring function calls | ||
-- AT LEAST at first level | ||
-- This means it will only parse function calls in sublists thanks to parseExpr | ||
parseExprList :: [Cpt] -> [Expr] | ||
parseExprList [] = [] | ||
parseExprList (x : xs) = case x of | ||
Sym str -> Symbole str : parseExprList xs | ||
Val i -> Num i : parseExprList xs | ||
List ls -> parseExpr ls : parseExprList xs | ||
|
||
-- Parses a CPT into a single Expr value | ||
parseExpr :: [Cpt] -> Expr | ||
parseExpr (Sym str : xs) = if isValidBuiltin str then | ||
Call str (parseExprList xs) else ExprList (parseExprList (Sym str : xs)) | ||
parseExpr ls = ExprList (parseExprList ls) | ||
|
||
-- Utility function for execution | ||
-- Converts cpt list to Expr Call | ||
-- IMPORTANT : Returns nothing in case of error | ||
exprListToCall :: [Expr] -> Maybe Expr | ||
exprListToCall [] = Nothing | ||
exprListToCall (Symbole name : xs) = Just (Call name xs) | ||
exprListToCall _ = Nothing | ||
|
||
-- DEFINITIONS | ||
-- Expression = Anything that ends up a value | ||
-- Function Call = When first element of list is symbol | ||
|
||
-- NOTES | ||
-- DID NOT implement ternary (if else) statement for the time being | ||
-- At execution : if list is deemed to be a function call, create function to convert it | ||
|
||
-- This is where we put builtins | ||
isValidBuiltin :: String -> Bool | ||
isValidBuiltin "define" = True | ||
isValidBuiltin "lambda" = True | ||
isValidBuiltin "add" = True | ||
isValidBuiltin "sub" = True | ||
isValidBuiltin "div" = True | ||
isValidBuiltin "mul" = True | ||
isValidBuiltin "noop" = True | ||
isValidBuiltin _ = False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
module Parsing.Token where | ||
|
||
import Data.Maybe | ||
import Text.Read | ||
|
||
-- ─── Tokenization ──────────────────────────────────────────────────────────────────────────────── | ||
|
||
-- Token Datatype | ||
data Token = OpenScope -- Opening parenthesis | ||
| CloseScope -- Closing parenthesis | ||
| Num Integer | ||
| Keyword String | ||
deriving (Show, Eq) | ||
|
||
-- Parse token from string | ||
parseToken :: String -> Token | ||
parseToken "(" = OpenScope | ||
parseToken ")" = CloseScope | ||
parseToken input = case readMaybe input :: Maybe Integer of | ||
Just x -> Num x | ||
Nothing -> Keyword input | ||
|
||
-- Function that tokenizes string | ||
-- | ||
-- Tokens are : ' ', '\n', '(', ')' | ||
-- Args are : Input -> Temp Str -> Output List | ||
tokenize' :: String -> String -> [Token] | ||
tokenize' [] "" = [] | ||
tokenize' [] str = [parseToken str] | ||
tokenize' (' ':xs) "" = tokenize' xs "" | ||
tokenize' (' ':xs) str = parseToken str : tokenize' xs "" | ||
tokenize' ('\n':xs) "" = tokenize' xs "" | ||
tokenize' ('\n':xs) str = parseToken str : tokenize' xs "" | ||
tokenize' ('(':xs) "" = OpenScope : tokenize' xs "" | ||
tokenize' ('(':xs) str = parseToken str : tokenize' ('(':xs) "" | ||
tokenize' (')':xs) "" = CloseScope : tokenize' xs "" | ||
tokenize' (')':xs) str = parseToken str : tokenize' (')':xs) "" | ||
tokenize' (x:xs) str = tokenize' xs (str <> [x]) | ||
|
||
-- Utility entry point function | ||
tokenize :: String -> [Token] | ||
tokenize str = tokenize' str "" | ||
|
||
-- Function to tokenize a given file | ||
-- | ||
-- Args : path | ||
tokenizeFile :: String -> IO [Token] | ||
tokenizeFile path = do | ||
-- Read file and Tokenize | ||
fileStr <- readFile path | ||
|
||
-- Return tokenization result | ||
return (tokenize fileStr) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.