Skip to content
Snippets Groups Projects
Commit 41d93e19 authored by ik1g19's avatar ik1g19
Browse files

add ex1 and ex2

parents
Branches
No related tags found
No related merge requests found
Showing
with 307 additions and 0 deletions
zipL :: ([Int],[Int]) -> [[Int]]
zipL (xs,ys) | length xs /= length ys = [[]]
zipL ([],[]) = []
zipL (x:xs,y:ys) = [x,y] : zipL (xs,ys)
\ No newline at end of file
.stack-work/
*~
\ No newline at end of file
# Changelog for exercise2stack
## Unreleased changes
Copyright ik (c) 2021
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of ik nor the names of other
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# exercise2stack
import Distribution.Simple
main = defaultMain
File added
File added
import Tokens
import System.Environment
import Control.Exception
import System.IO
main :: IO ()
main = catch main' noLex
main' = do (fileName : _ ) <- getArgs
sourceText <- readFile fileName
putStrLn ("Lexing : " ++ sourceText)
let lexedProg = alexScanTokens sourceText
putStrLn ("Lexed as " ++ (show lexedProg))
noLex :: ErrorCall -> IO ()
noLex e = do let err = show e
hPutStr stderr ("Problem with Lexing : " ++ err)
return ()
\ No newline at end of file
{
module Tokens where
}
%wrapper "basic"
$digit = 0-9
-- digits
$alpha = [a-zA-Z]
-- alphabetic characters
tokens :-
$white+ ;
"--".* ;
let { \s -> TokenLet }
in { \s -> TokenIn }
$digit+ { \s -> TokenInt (read s) }
\= { \s -> TokenEq }
\+ { \s -> TokenPlus }
\- { \s -> TokenMinus }
\* { \s -> TokenTimes }
\/ { \s -> TokenDiv }
\^ { \s -> TokenExp }
\( { \s -> TokenLParen }
\) { \s -> TokenRParen }
$alpha [$alpha $digit \_ \']* { \s -> TokenVar s }
{
-- Each action has type :: String -> Token
-- The token type:
data Token =
TokenLet |
TokenIn |
TokenInt Int |
TokenVar String |
TokenEq |
TokenPlus |
TokenMinus |
TokenTimes |
TokenDiv |
TokenExp |
TokenLParen |
TokenRParen
deriving (Eq,Show)
}
\ No newline at end of file
File added
import Tokens
import System.Environment
import Control.Exception
import System.IO
main :: IO ()
main = catch main' noLex
main' = do (fileName : _ ) <- getArgs
sourceText <- readFile fileName
putStrLn ("Lexing : " ++ sourceText)
let lexedProg = (alexScanTokens sourceText)
putStrLn ("lexed as " ++ (show lexedProg))
noLex :: ErrorCall -> IO ()
noLex e = do let err = show e
hPutStr stderr ("Problem with lexing: " ++ err)
return ()
\ No newline at end of file
{
module Tokens where
}
%wrapper "posn"
$digit = 0-9
-- digits
$alpha = [a-zA-Z]
-- alphabetic characters
tokens :-
$white+ ;
"--".* ;
let { \p s -> TokenLet p}
in { \p s -> TokenIn p }
$digit+ { \p s -> TokenInt p (read s) }
\= { \p s -> TokenEq p }
\+ { \p s -> TokenPlus p }
\- { \p s -> TokenMinus p }
\* { \p s -> TokenTimes p }
\/ { \p s -> TokenDiv p }
\^ { \p s -> TokenExp p }
\( { \p s -> TokenLParen p }
\) { \p s -> TokenRParen p }
$alpha [$alpha $digit \_ \’]* { \p s -> TokenVar p s }
{
-- Each action has type :: AlexPosn -> String -> Token
-- The token type:
data Token =
TokenLet AlexPosn |
TokenIn AlexPosn |
TokenInt AlexPosn Int |
TokenVar AlexPosn String |
TokenEq AlexPosn |
TokenPlus AlexPosn |
TokenMinus AlexPosn |
TokenTimes AlexPosn |
TokenDiv AlexPosn |
TokenExp AlexPosn |
TokenLParen AlexPosn |
TokenRParen AlexPosn
deriving (Eq,Show)
tokenPosn :: Token -> String
tokenPosn (TokenInt (AlexPn a l c) n) = show(l) ++ ":" ++ show(c)
tokenPosn (TokenVar (AlexPn a l c) x) = show(l) ++ ":" ++ show(c)
tokenPosn (TokenLet (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn (TokenIn (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn (TokenEq (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn (TokenPlus (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn (TokenMinus (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn (TokenTimes (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn (TokenDiv (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn (TokenExp (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn (TokenLParen (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn (TokenRParen (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
}
\ No newline at end of file
File added
{
module MDLTokens where
}
%wrapper "posn"
$digit = 0-9
-- digits
tokens :-
$white+ ;
"--".* ;
Forward { (\p s -> TokenForward p)}
Rotate { (\p s -> TokenRotate p) }
[1-9] { (\p s -> TokenDigit p (read s)) }
$digit $digit+ { (\p s -> TokenInt p (read s)) }
Check { (\p s -> TokenCheck p) }
If { (\p s -> TokenIf p) }
Then { (\p s -> TokenThen p) }
Else { (\p s -> TokenElse p) }
L { (\p s -> TokenLeft p) }
R { (\p s -> TokenRight p) }
\; { (\p s -> TokenSeq p )}
\( { (\p s -> TokenLParen p) }
\) { (\p s -> TokenRParen p) }
{
-- Each action has type :: AlexPosn -> String -> MDLToken
-- The token type:
data MDLToken =
TokenForward AlexPosn |
TokenRotate AlexPosn |
TokenDigit AlexPosn Int |
TokenInt AlexPosn Int |
TokenCheck AlexPosn |
TokenIf AlexPosn |
TokenThen AlexPosn |
TokenElse AlexPosn |
TokenLeft AlexPosn |
TokenRight AlexPosn |
TokenSeq AlexPosn |
TokenLParen AlexPosn |
TokenRParen AlexPosn
deriving (Eq,Show)
tokenPosn :: MDLToken -> String
tokenPosn (TokenForward (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn (TokenRotate (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn (TokenDigit (AlexPn a l c) _) = show(l) ++ ":" ++ show(c)
tokenPosn (TokenInt (AlexPn a l c) _) = show(l) ++ ":" ++ show(c)
tokenPosn (TokenCheck (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn (TokenIf (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn (TokenThen (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn (TokenElse (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn (TokenLeft (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn (TokenRight (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn (TokenSeq (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn (TokenLParen (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn (TokenRParen (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
}
\ No newline at end of file
import MDLTokens
import System.Environment
import Control.Exception
import System.IO
main :: IO ()
main = catch main' noParse
main' = do (fileName : _ ) <- getArgs
sourceText <- readFile fileName
putStrLn ("Parsing : " ++ sourceText)
let parsedProg = parseCalc (alexScanTokens sourceText)
putStrLn ("Parsed as " ++ (show parsedProg))
noParse :: ErrorCall -> IO ()
noParse e = do let err = show e
hPutStr stderr err
return ()
\ No newline at end of file
Forward 10 ;
Rotate L ;
If (Check 3)
Then Rotate L ; Forward 1 ; Rotate R
Else Forward 3 ; Rotate L
Forward 10 ;
Rotate L ;
If (Check 3)
Then Rotate L ; Forward 1 ; Rotate R
Else Forward 3 ; Rotate L
Rotate L
module Main where
import Tokens
import System.Environment
import Control.Exception
import System.IO
main :: IO ()
main = catch main' noLex
main' = do (fileName : _) <- getArgs
sourceText <- readFile fileName
putStrLn $ "Lexing: " ++ sourceText
let lexedProg = alexScanTokens sourceText
putStrLn $ "Lexed as: " ++ (show lexedProg)
noLex :: ErrorCall -> IO ()
noLex e = do let err = show e
hPutStrLn stderr ("Problem with lexing :" ++ err)
return ()
\ No newline at end of file
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment