From 84f498de81de32532d5d45d4f7f5102e49862cf6 Mon Sep 17 00:00:00 2001 From: vijayphoenix Date: Tue, 8 Sep 2020 13:08:46 +0530 Subject: [PATCH] Documentation update --- .circleci/config.yml | 17 ----------------- LICENSE | 22 ++++++++++++++++++++++ README.md | 27 +++++++++++++-------------- examples.txt | 27 +++++++++++++++++++++++++++ package.yaml | 10 +++++----- src/AST.hs | 13 ++++++------- test/Spec.hs | 15 +++++---------- 7 files changed, 78 insertions(+), 53 deletions(-) delete mode 100644 .circleci/config.yml create mode 100644 LICENSE create mode 100644 examples.txt diff --git a/.circleci/config.yml b/.circleci/config.yml deleted file mode 100644 index 9af4120..0000000 --- a/.circleci/config.yml +++ /dev/null @@ -1,17 +0,0 @@ -version: 2.0 -jobs: - build: - docker: - - image: fpco/stack-build:lts - steps: - - checkout - - run: - name: Set up - command: | - sudo apt-get -y update - sudo add-apt-repository -y ppa:hvr/ghc - sudo apt-get install -y haskell-platform - wget -O - http://llvm.org/apt/llvm-snapshot.gpg.key|sudo apt-key add - - alias llvm-config="llvm-config-4.0" - sudo apt-get -y install llvm-4.0 llvm-4.0-dev - stack build diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..a265916 --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ + +MIT License + +Copyright (c) 2019 Vijay Tadikamalla + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md index c796a56..285ed73 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ -[![CircleCI](https://circleci.com/gh/IITH-SBJoshi/haskell-11/tree/master.svg?style=svg&circle-token=72b4ba1854966fc5f4e58b1e3d0fc5a05cf4ad66)](https://circleci.com/gh/IITH-SBJoshi/haskell-11/tree/master) +[![Build Status](https://travis-ci.com/vijayphoenix/Compiler-written-in-Haskell.svg?token=67qmZmyfex1ST6G5tpZK&branch=master)](https://travis-ci.com/vijayphoenix/Compiler-written-in-Haskell) # Implementing a JIT Compiled Language with Haskell ## About -The aim of the project was to implement a simple procedural language. +The project aimed to implement a simple procedural language. We named it **HASKULL** :-) The frontend is written in Haskell and the backend it managed LLVM-hs-pure package. The project extensively uses Monads, State Monads, Applicative functors and Transformers. @@ -18,7 +18,7 @@ You will need GHC 7.8 or newer as well as LLVM 4.0. Clone the repository: ``` -git clone "https://github.com/IITH-SBJoshi/haskell-11.git" +git clone https://github.com/vijayphoenix/Compiler-written-in-Haskell.git ``` Browse to the directory where all the files of this repository are located. @@ -37,7 +37,7 @@ stack repl ``` Type "main" in the interactive console. -Write any code using following Syntax rules +Write any code using the following Syntax rules. ``` Syntax rules: @@ -47,14 +47,13 @@ All the symbol starting with lower-case letter are terminal(lexer units). All the operators are left associative Command = Expr ; -Expr : DeclarationStmt | FuncCallStmt | LiteralStmt | Var | ifthenStmt | (Expr) +Expr : DeclarationStmt| FuncCallStmt | LiteralStmt | ifthenStmt | (Expr) -DeclarationStmt : ExternDecl | VarDecl +DeclarationStmt : ExternDecl ExternDecl : extern Name([ArgList]) : Type -VarDecl : Type VList -Type : int | string +Type : int VList: Name[, VList] FuncCallStmt : Call @@ -63,13 +62,12 @@ Call : Name ( [Args] ) BinOpCallStmt : BinOpCall BinOpCall : Expr Op Expr -Op : + | - | * | / | ; | = | < +Op : + | - | * | / | ; | < reserved keywords: int char def extern string if then else Args : Expr[, Args] -LiteralStmt : StrLiteral | IntLiteral +LiteralStmt : IntLiteral IntLiteral : integer -StrLiteral : string Name : ident ArgList : Type Name[, ArgList] @@ -79,7 +77,8 @@ Command-list = Command [Command-list] Command = Expr ; ``` -For more insight on the language grammer, refer to Language.hs, Lexer.hs, Parser.hs files. +Some sample example functions are provided in examples.txt file. +For more insight on the language grammar, refer to Language.hs, AST.hs files. ### Documentation @@ -100,7 +99,7 @@ stack haddock * [**Yogesh Singh**](https://github.com/yo5sh) #### License -* [LICENSE](https://github.com/IITH-SBJoshi/haskell-11/blob/master/LICENSE) +* [LICENSE](LICENSE) #### Acknowledgments -* [Haskell LLVM JIT Compiler Tutorial](http://www.stephendiehl.com/llvm) \ No newline at end of file +* [Haskell LLVM JIT Compiler Tutorial](http://www.stephendiehl.com/llvm) diff --git a/examples.txt b/examples.txt new file mode 100644 index 0000000..b66e88f --- /dev/null +++ b/examples.txt @@ -0,0 +1,27 @@ +Some example functions: + +def eq(int x, int y):int { if(x-y) then {0} else {1};} + +extern int abs(int x):int ; + +def leq(int x, int y) : int { if(x) then { if (y) then { leq(x-1, y-1) } else {1} } else {2}; } {- | x<=y | 2-> y>=x | 1-> x>y | -} +def gt(int x, int y) : int { if( eq( leq(int x, int y), 1 ) ) then {1} else {2} ;} {- | x>y ? 1 : 2 | -} + +def fib(int x):int { if(x-1) then { if (x) then { fib(x-1)+fib(x-2) } else {0} } else {1}; } {- | x=0 ? 1 : x*(x-1)! | -} +def fact(int x):int { if(x) then { x*fact(x-1) } else {1}; } + +def mod(int x, int y): int { x - (x/y)*y; } +def pow(int x, int y): int { if(y) then { x*pow(x, y-1) } else {1} ; } +def powmod(int x, int y, int p): int { if(y) then { mod( x * powmod(x, y-1), p) } else {1}; } +def nCr(int n, int r): int { fact(n)/fact(n-r)/fact(r);} + +4 + 5*6 - 3*2 + 10/3 + 5*7; + + + +def pred(int x) : int { x-1 ;} +def succ(int x) : int { x+1 ;} + + +def and(int x, int y) : int { if( eq( x+y , 2 ) ) then {1} else {0} ;} +def or (int x, int y) : int { if( eq( x+y , 0 ) ) then {0} else {1} ;} diff --git a/package.yaml b/package.yaml index 8321f16..d0cf6ae 100644 --- a/package.yaml +++ b/package.yaml @@ -1,10 +1,10 @@ name: Haskull version: 0.1.0.0 -github: "https://github.com/IITH-SBJoshi/haskell-11" -license: BSD3 -author: "Vijay" +github: "https://github.com/vijayphoenix/Compiler-written-in-Haskell" +license: MIT +author: "Vijay Tadikamalla" maintainer: "cs17btech11040@iith.ac.in" -copyright: "2019 Vijay" +copyright: "2019 Vijay Tadikamalla" extra-source-files: - README.md @@ -17,7 +17,7 @@ extra-source-files: # To avoid duplicated efforts in documentation and dealing with the # complications of embedding Haddock markup inside cabal files, it is # common to point users to the README.md file. -description: Please see the README on GitHub at +description: Please see the README on GitHub at dependencies: - base >= 4.7 && < 5 diff --git a/src/AST.hs b/src/AST.hs index a470970..cb673c1 100644 --- a/src/AST.hs +++ b/src/AST.hs @@ -22,21 +22,20 @@ module AST ( -- All the operators are left associative -- Command = Expr ; --- Expr : DeclarationStmt | FuncCallStmt | LiteralStmt | (Expr) +-- Expr : DeclarationStmt | FuncCallStmt | LiteralStmt | IfthenStmt | (Expr) --- DeclarationStmt : ExternDecl | VarDecl +-- DeclarationStmt : ExternDecl -- ExternDecl : extern Name([ArgList]) : Type --- VarDecl : Type VList --- Type : int | string +-- Type : int -- VList: Name[, VList] --- FuncCallStmt : BinOpCall | Call +-- FuncCallStmt : Call -- BinOpCall : Expr Op Expr -- Call : Name ( [Args] ) --- Op : + | - | * | / +-- Op : + | - | * | / | < -- Args : Expr[, Args] -- LiteralStmt : StrLiteral | IntLiteral @@ -71,7 +70,7 @@ data Expr | BinOpCallStmt BinOpCall -- Done | LiteralStmt Literal -- Done | Var Name -- Done - | IfthenStmt Ifthen -- Left + | IfthenStmt Ifthen -- Done deriving (Show) diff --git a/test/Spec.hs b/test/Spec.hs index 701e5ab..12df325 100644 --- a/test/Spec.hs +++ b/test/Spec.hs @@ -16,30 +16,25 @@ main func2 && func3 && func4 && - func5 && - func6 = putStrLn "\nTEST PASSING" + func5 = putStrLn "\nTEST PASSING" | otherwise = error "TESTS NOT PASSING" func1 = case (parse binOpCallStmtParser "File1" "5+3;") of Right s -> (show (parse binOpCallStmtParser "File1" "5+3;")) == "Right (BinOpCallStmt (BinOpCall {op = Plus, lhs = LiteralStmt (IntLiteral 5), rhs = LiteralStmt (IntLiteral 3)}))" Left err -> error $ show err -func2 = case (parse binOpCallStmtParser "File2" "4+5*7/10;") of - Right s -> (show (parse binOpCallStmtParser "File2" "4+5*7/10;")) == "Right (BinOpCallStmt (BinOpCall {op = Divide, lhs = BinOpCallStmt (BinOpCall {op = Plus, lhs = LiteralStmt (IntLiteral 4), rhs = BinOpCallStmt (BinOpCall {op = Mul, lhs = LiteralStmt (IntLiteral 5), rhs = LiteralStmt (IntLiteral 7)})}), rhs = LiteralStmt (IntLiteral 10)}))" - Left err -> error $ show err - -func3 = case (parse binOpCallStmtParser "File2" "x+y;") of +func2 = case (parse binOpCallStmtParser "File2" "x+y;") of Right s -> (show (parse binOpCallStmtParser "File2" "x+y;")) == "Right (BinOpCallStmt (BinOpCall {op = Plus, lhs = Var \"x\", rhs = Var \"y\"}))" Left err -> error $ show err -func4 = case (parse literalStmtParser "File2" "\"Hello\"") of +func3 = case (parse literalStmtParser "File2" "\"Hello\"") of Right s -> (show (parse literalStmtParser "File2" "\"Hello\"")) == "Right (LiteralStmt (StrLiteral \"Hello\"))" Left err -> error $ show err -func5 = case (parse literalStmtParser "File2" "15000") of +func4 = case (parse literalStmtParser "File2" "15000") of Right s -> (show (parse literalStmtParser "File2" "15000")) == "Right (LiteralStmt (IntLiteral 15000))" Left err -> error $ show err -func6 = case (parse funcCallStmtParser "File2" "func(4,3)") of +func5 = case (parse funcCallStmtParser "File2" "func(4,3)") of Right s -> (show (parse funcCallStmtParser "File2" "func(4,3)")) == "Right (FuncCallStmt (FuncCall {callee = \"func\", args = [LiteralStmt (IntLiteral 4),LiteralStmt (IntLiteral 3)]}))" Left err -> error $ show err