From 889d4798a0da64d76422da98378f5e5c35f0bf32 Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Fri, 12 Feb 2016 18:27:05 +0100 Subject: [PATCH] completed example, added != token --- example.spl | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++ lex.dcl | 1 + lex.icl | 4 ++- 3 files changed, 88 insertions(+), 1 deletion(-) diff --git a/example.spl b/example.spl index f7ce562..58d037e 100644 --- a/example.spl +++ b/example.spl @@ -9,3 +9,87 @@ facR(n) :: Int -> Int { return n * facR(n-1); } } + +//The iterative version of the factorial function +facl ( n ) :: Int -> Int { + var r = 1; + while(n>1){ + r = r*n; + n = n-1; + } + return r; +} + +//A main function to check the results +//It takes no arguments, so the type looks like this: +main ( ) :: Void { + var n = 0; + var facN = 1; + var ok = True; + while(n<20) { + facN = facR (n); + if (facN != factl (n) || facn != facL (n)){ + print (n : facN : facl (n) : facL (n): []); + ok=False; + } + } + print(ok); +} + +// A list based factorial function +// Defined here to show that functions can be given in any order (unlike C) +facL (n) :: Int -> Int { + return product (fromTo(1, n) ); //Inline comments yay +} + +//Generates a list of integers from the first to the last argument +fromTo (from, to) :: Int Int -> [Int] { + if(from <= to){ + return from:fromTo(from+1, to); + } else { + return []; + } +} + +//Make a reversed copy of any list +reverse(list):: [t] -> [t] { + var accu = []; + while(!isEmpty(list)){ + accu = list.hd:accu; + list = list.tl; + } + return accu; +} + +//Absolute value, in a strange layout +abs(n)::Int->Int{if(n<0)return -n; else return n;} + +//swap the elements in a tuple +swap(tuple) :: (a, a) -> (a, a){ + var tmp = tuple.fst; + tuple.fst = tuple.snd; + tuple.snd = tmp; + return tuple; +} + +//list append +append(l1, l2) :: [t] [t] -> [t] { + if(isEmpty(l1)){ + return l2; + } else { + l1.tl = append(l1.tl, l2); + return l1; + } +} + +//square the odd numbers in a list and remove the even members +squareOddNumbers(list) :: [Int] -> [Int] { + while(!isEmpty (list) && list.hd % 2==0){ + list=list.tl; + } + if(!isEmpty(list)){ + list.hd = list.hd*list.hd; + list.tl = squareOddNumbers(list.tl); + } + return list; +} diff --git a/lex.dcl b/lex.dcl index d6c421c..d1daf7c 100644 --- a/lex.dcl +++ b/lex.dcl @@ -21,6 +21,7 @@ import Data.Either | BoolTypeToken // Bool keyword //Two character tokens | DoubleColonToken // :: + | NotEqualToken // != | LesserEqToken // <= | GreaterEqToken // >= | EqualsToken // == diff --git a/lex.icl b/lex.icl index aa50e72..2eeb600 100644 --- a/lex.icl +++ b/lex.icl @@ -55,6 +55,7 @@ lex ['C':'h':'a':'r':rest] = (Right CharTypeToken, rest) lex ['B':'o':'o':'l':rest] = (Right BoolTypeToken, rest) //Two character tokens lex [':':':':rest] = (Right DoubleColonToken, rest) +lex ['!':'=':rest] = (Right NotEqualToken, rest) lex ['<':'=':rest] = (Right LesserEqToken, rest) lex ['>':'=':rest] = (Right GreaterEqToken, rest) lex ['=':'=':rest] = (Right EqualsToken, rest) @@ -115,7 +116,7 @@ print i [(CharToken c):rest] = ["'":toString c:"'":print i rest] print i [VarToken:rest] = ["var ":print i rest] print i [ReturnToken:rest] = ["return ":print i rest] print i [IfToken:rest] = ["if":print i rest] -print i [ElseToken:rest] = ["else":print i rest] +print i [ElseToken:rest] = ["else ":print i rest] print i [WhileToken:rest] = ["while":print i rest] print i [TrueToken:rest] = ["True":print i rest] print i [FalseToken:rest] = ["False":print i rest] @@ -124,6 +125,7 @@ print i [IntTypeToken:rest] = ["Int":print i rest] print i [CharTypeToken:rest] = ["Char":print i rest] print i [BoolTypeToken:rest] = ["Bool":print i rest] print i [DoubleColonToken:rest] = [" :: ":print i rest] +print i [NotEqualToken:rest] = [" != ":print i rest] print i [LesserEqToken:rest] = [" <= ":print i rest] print i [GreaterEqToken:rest] = [" >= ":print i rest] print i [EqualsToken:rest] = [" == ":print i rest] -- 2.20.1