Skip to content

Commit

Permalink
fix bug in prelude
Browse files Browse the repository at this point in the history
  • Loading branch information
Haoqiang Fan committed Dec 27, 2016
1 parent 25cbc3f commit b7375c3
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion UData.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ showBCompType BNEq = "/="

applyBVal :: BValue -> BValue -> BResult
applyBVal (BFuncVal b) (BNumVal v) = applyBFunc b v
applyBVal (BFuncVal b) _ = BException ("cannot apply non-numeric argument to builtin function "++(show b))
applyBVal (BFuncVal b) _ = BException ("cannot apply built-in function "++(showBFunc b)++" on non-numeric value")
applyBVal (BIntList args) (BNumVal (BInt v)) = BClean (BIntList (v:args))
applyBVal (BIntList args) _ = BException "cannot append non-integer number in IntList"
applyBVal _ _ = BException "cannot use numeric value as function"
Expand Down
2 changes: 1 addition & 1 deletion URunTime.hs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ applyFunc ((VBuiltin a),ca) br = case a of
_ -> case br of
(VException e) -> VException e
(VGood (VBuiltin b,_)) -> bValToVResult (applyBVal a b)
(VGood x) -> VException ("cannot feed non-builtin value to builtin value "++(showBValue a))
(VGood x) -> VException ("cannot apply built-in function "++(showBValue a)++" on non-numeric value")
where
bValToVResult (BException e) = VException e
bValToVResult (BClean v) = VGood (VBuiltin v,emptyContext)
Expand Down
6 changes: 3 additions & 3 deletions aplusb.u
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
(import* io)
(import* str)
(run
(a readInt)
(b readInt)
(_ (putStrLn (itoa (+ a b))))
(line readLine)
(let s (sum (map atoi (splitStr ' ' (rstrip line)))))
(_ (putStrLn (itoa s)))
(exit 0)
)
9 changes: 9 additions & 0 deletions io.u
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@
)
))
(def readInt (readIntF 0))
(def readLineF (\handle
(do
(body (readWhileF handle (/= '\n')))
(trail (readIfF handle(= '\n')))
(let body_t (trail body (\x (++ body (cons x empty)))))
(return body_t)
)
))
(def readLine (readLineF 0))

; getArg
(let getArgStr (recur (\f (do
Expand Down
7 changes: 5 additions & 2 deletions prelude.u
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
(def True (\x y x))
(def False (\x y y))
(def if id)
(def not (\x False True))
(def not (\x (x False True)))
(def and (\x y (x y False)))
(def or (\x y (x True y)))

Expand All @@ -24,6 +24,7 @@
; the List protocol: (l null_value (\head tail list_value))
(def cons (\x xs f g (g x xs)))
(def empty fst)
(def null (\x (x True (\_ _ False))))
(def foldr (\f x0 (recur (\g (\l
(l x0 (\x xs
(f x (g xs))
Expand All @@ -42,7 +43,9 @@
))))
))
(def ++ (\a b (foldr (\x y (cons x y)) b a)))
(def join (\sep (foldr1 (\x y (++ x (++ sep y))) id "")))
(def join (\sep (foldr1 (\x y (++ x (++ sep y))) id empty)))
(def sum (foldl + 0))
(def map (\f (foldr (\h r (cons (f h) r)) empty)))

; the Maybe protocol: (m nothing_value (\v just_value))
(def nothing fst)
Expand Down
1 change: 1 addition & 0 deletions str.u
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@
)
) empty s)
)))
(def rstrip (foldr (\a r (or (not (isSpace a)) (not (null r)) (cons a r) r)) empty))

2 changes: 2 additions & 0 deletions test.u
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
(import* str)
(import* io)
(run
;(line readLine)
;(_ (putStrLn line))
;(r (system "ls -l"))
;(_ (putStrLn (itoa r)))

Expand Down

0 comments on commit b7375c3

Please sign in to comment.