Skip to content

Commit

Permalink
Change syntax for function and return types
Browse files Browse the repository at this point in the history
  • Loading branch information
rockofox committed Dec 15, 2024
1 parent cc210db commit 78113f5
Show file tree
Hide file tree
Showing 26 changed files with 170 additions and 172 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Inspired by F# and Haskell, it aims to deliver satisfactory functional programmi
```julia
struct Person = (name: String, age: Int)

let greet (p: Person) => IO = println "Hello " : p.name
let main => IO = do
let greet (p: Person): IO = println "Hello " : p.name
let main: IO = do
let person = Person { name: "Lily", age: 22 }
greet person
end
Expand Down
4 changes: 2 additions & 2 deletions examples/bottles.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
let bottles (i: Int) => IO = do
let bottles (i: Int) : IO = do
if i > 0 then do
println ^i : " bottles of beer on the wall, " : ^i : " bottles of beer."
println "Take one down and pass it around, " : ((i) - 1) as String : " bottles of beer on the wall.\n"
Expand All @@ -9,6 +9,6 @@ let bottles (i: Int) => IO = do
end
end

let main => IO = do
let main : IO = do
bottles 99
end
2 changes: 1 addition & 1 deletion examples/casting.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ external "__default" = do
sin :: Double -> Double
end

let main => IO = do
let main : IO = do
let tick = $20
println 100.0 as Double * (sin (tick as Double))
end
2 changes: 1 addition & 1 deletion examples/cities.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# let main => IO = do
# let main : IO = do
let cities = ["London", "Oslo", "Paris", "Amsterdam", "Berlin"]
let visited = ["Berlin", "Oslo"]
println map (\a -> "I still need to visit " : a : "!" : "\n"), ((cities)-(visited))
Expand Down
2 changes: 1 addition & 1 deletion examples/ffi_strlen.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ external "__default" = do
strlen :: String -> Int
end

let main => IO = do
let main : IO = do
println strlen "Hello, World!\n"
end
10 changes: 5 additions & 5 deletions examples/fib.in
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
let fib (0: Int) => Int = 0
let fib (1: Int) => Int = 1
let fib (n: Int) => Int = (fib ((n) - 1)) + (fib ((n) - 2))
let fib (0: Int) : Int = 0
let fib (1: Int) : Int = 1
let fib (n: Int) : Int = (fib ((n) - 1)) + (fib ((n) - 2))

#
# let fib (n:Int) => Int = do
# let fib (n:Int) : Int = do
# if n == 0 then
# 0
# else if n == 1 then
Expand All @@ -16,6 +16,6 @@ let fib (n: Int) => Int = (fib ((n) - 1)) + (fib ((n) - 2))
# end
# end

let main => IO = do
let main : IO = do
println fib 12
end
6 changes: 3 additions & 3 deletions examples/generics.in
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ trait Number
impl Number for Int
impl Number for Float

let kiss<T: Animal> (a: T b: T) => IO = do
let kiss<T: Animal> (a: T b: T) : IO = do
println a.name
end

let bla<T: Number> (a: T b: T) => T = do
let bla<T: Number> (a: T b: T) : T = do
a + b
end

let main => IO = do
let main : IO = do
let a = Dog { name: "dog" }
let b = Cat { name: "cat" }
let square (x: Int) = x * x
Expand Down
2 changes: 1 addition & 1 deletion examples/guess.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
let main => IO = do
let main : IO = do
let number = randomInt 1, 10
println "Guess a number between 1 and 10"
let guess = $getLine
Expand Down
2 changes: 1 addition & 1 deletion examples/kitchen-sink.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
let main => IO = do
let main : IO = do
let bla = \x -> x
end
2 changes: 1 addition & 1 deletion examples/lambdas.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
let main => IO = do
let main : IO = do
let square (x:Int) = x * x
let squareAll (xs:[Int]) = square 2
let strict = $12
Expand Down
2 changes: 1 addition & 1 deletion examples/pi.in
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ pi n = do
4.0 * rsum
end

let main => IO = do
let main : IO = do
println pi 4000.0
end
4 changes: 2 additions & 2 deletions examples/raylib.in
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ let screenHeight = 450
struct RaylibColor = (r: Char, g: Char, b: Char, a: Char)
struct Vector3 = (x: Float, y: Float, z: Float)
/* test */
let loop (tick: Int) => IO = do
let loop (tick: Int) : IO = do
BeginDrawing
ClearBackground 0
times 7, (\i -> do
Expand All @@ -48,7 +48,7 @@ let loop (tick: Int) => IO = do
else do
end
end
let main => IO = do
let main : IO = do
InitWindow screenWidth, screenHeight, "Hello Indigo"
SetTargetFPS 60
loop 0
Expand Down
14 changes: 8 additions & 6 deletions examples/state.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@ end

`:=` :: String -> Any -> State
`:=` n v = do
if n == "x" then do
State{x: v}
else do
State{y: v}
end
update State{}, n, v
end

testState :: State
testState = do
"x" := 123
"y" := 4562
"x" := 1232
# liftM (println "Hello, world!")
end

main :: IO
main = println testState
# main = println testState
main = do
testCase "abc", (\x -> do
assertEquals "x", "y"
end)
end
2 changes: 1 addition & 1 deletion examples/sum.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mysum :: [Float] -> Float
mysum [] = 0.0
mysum (x:xs) = x + mysum xs

let main => IO = do
let main : IO = do
let xs = range 1.0,100.0,2.0
# print (sum xs)
print mysum xs
Expand Down
2 changes: 1 addition & 1 deletion examples/sum2.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mysum :: [Float] -> Float
mysum [] = 0.0
mysum (x:xs) = x + mysum xs

let main => IO = do
let main : IO = do
let xs = range 1.0,100.0,2.0
# print (sum xs)
print mysum xs
Expand Down
4 changes: 2 additions & 2 deletions examples/sum_types.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ struct Car = ()
impl Animal for Cat
impl Animal for Dog

let pet (a: Animal) => IO = do
let pet (a: Animal) : IO = do
println "petting animal"
end

let main => IO = do
let main : IO = do
pet Cat {}
pet Dog {}
pet Car {}
Expand Down
2 changes: 1 addition & 1 deletion examples/traits.in
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ impl Animal for Cat = do
makeNoise self = println "Meow"
end

let main => IO = do
let main : IO = do
makeNoise (Dog {})
makeNoise (Cat {})
end
4 changes: 2 additions & 2 deletions lib/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ instance Show Type where
show Any = "Any"
show None = "None"
show Unknown = "Unknown"
show (Fn fnArgs fnRet) = "Fn{" ++ show fnArgs ++ " -> " ++ show fnRet ++ "}"
show (List t) = "List{" ++ show t ++ "}"
show (Fn fnArgs fnRet) = "(" ++ show fnArgs ++ " -> " ++ show fnRet ++ ")"
show (List t) = "[" ++ show t ++ "]"
show (StructT structName) = structName
show Self = "Self"

Expand Down
12 changes: 5 additions & 7 deletions lib/BytecodeCompiler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -596,13 +596,11 @@ compileExpr (Parser.Import{objects = o, from = from, as = as, qualified = qualif
let expr = case parseProgram (T.pack i) Parser.initCompilerFlags{Parser.needsMain = False} of -- FIXME: pass on flags
Left err -> error $ "Parse error: " ++ errorBundlePretty err
Right (Parser.Program exprs) -> exprs
let p =
if qualified || isJust as
then do
let alias = if qualified then from else fromJust as
concatMapM (`compileExpr` expectedType) (map (`mangleAST` alias) expr)
else concatMapM (`compileExpr` expectedType) expr
p >>= \p' -> return p'
if qualified || isJust as
then do
let alias = if qualified then from else fromJust as
concatMapM (`compileExpr` expectedType) (map (`mangleAST` alias) expr)
else concatMapM (`compileExpr` expectedType) expr
where
mangleAST :: Parser.Expr -> String -> Parser.Expr
mangleAST (Parser.FuncDec name types _) alias = Parser.FuncDec (alias ++ "@" ++ name) types []
Expand Down
10 changes: 4 additions & 6 deletions lib/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,10 @@ validType =
keyword "Any"
return Any
<|> do
keyword "Fn"
curlyBrackets $ do
parens $ do
args <- sepBy validType (symbol "->")
symbol "=>"
ret <- validType
return Fn{args = args, ret = ret}
let ret = last args
return $ Fn args ret
<|> do
squareBrackets $ do
List <$> validType
Expand Down Expand Up @@ -338,7 +336,7 @@ combinedFunc = do
name <- identifier <|> gravis <?> "function name"
generics <- (fromMaybe [] <$> optional generic) <?> "function generics"
(args, argTypes) <- (parens argsAndTypes <|> argsAndTypes) <?> "function arguments"
returnType <- optional (symbol "=>" >> validType <?> "return type") <?> "return type"
returnType <- optional (symbol ":" >> validType <?> "return type") <?> "return type"
symbol "="
body <- recover expr <?> "function body"
return $ Function [FuncDef name args body] (FuncDec name (argTypes ++ [fromMaybe Any returnType]) generics)
Expand Down
Loading

0 comments on commit 78113f5

Please sign in to comment.