Skip to content

Commit

Permalink
Struct is shorthand syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
rockofox committed Jun 24, 2024
1 parent aab4251 commit 349d044
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ data Expr
| Discard Expr
| Import {objects :: [String], from :: String, qualified :: Bool, as :: Maybe String}
| Ref Expr
| Struct {name :: String, fields :: [(String, Type)], refinement :: Maybe Expr, refinementSrc :: String}
| Struct {name :: String, fields :: [(String, Type)], refinement :: Maybe Expr, refinementSrc :: String, is :: [String]}
| StructLit String [(String, Expr)] Position
| StructAccess Expr Expr
| ListLit [Expr]
Expand Down
3 changes: 2 additions & 1 deletion lib/BytecodeCompiler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -432,9 +432,10 @@ compileExpr (Parser.Cast from to) = do
compileType (Parser.Var "CPtr" _) = [Push $ DCPtr $ ptrToWordPtr nullPtr]
compileType (Parser.ListLit [x]) = compileType x ++ [PackList 1]
compileType x = error $ "Type " ++ show x ++ " is not implemented"
compileExpr st@(Parser.Struct{fields}) = do
compileExpr st@(Parser.Struct{name = structName, fields, is}) = do
modify (\s -> s{structDecs = st : structDecs s})
mapM_ createFieldTrait fields
mapM_ (compileExpr . (\t -> Parser.Impl{methods = [], for = structName, trait = t})) is
return []
where
createFieldTrait :: (String, Parser.Type) -> StateT (CompilerState a) IO ()
Expand Down
5 changes: 4 additions & 1 deletion lib/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,10 @@ struct = do
refinement <- optional $ do
keyword "satisfies"
parens expr
return $ Struct{name = name, fields = fields, refinement = refinement, refinementSrc = fromMaybe "" refinementSrc}
is <- optional $ do
keyword "is"
sepBy extra (symbol ",")
return $ Struct{name = name, fields = fields, refinement = refinement, refinementSrc = fromMaybe "" refinementSrc, is = fromMaybe [] is}
where
structField = do
fieldName <- identifier <?> "field name"
Expand Down
8 changes: 8 additions & 0 deletions tests/IntegrationSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,14 @@ spec = do
println name mauzi
end|]
`shouldReturn` "Mauzi\n"
it "Can use `is` syntax" $ do
compileAndRun
[r|
trait Person
trait Human
struct Person = (name: String, age: Int) is Person, Human
|]
`shouldReturn` ""
describe "Traits" $ do
it "Can use a trait" $ do
compileAndRun
Expand Down
9 changes: 9 additions & 0 deletions tests/ParserSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ spec = do
parseProgram "bello{}.name" parserCompilerFlags
`shouldBe` Right
(Program [StructAccess (StructLit "bello" [] anyPosition) (Var "name" anyPosition)])
it "Can define" $
parseProgram "struct Teacher = ()" parserCompilerFlags
`shouldBe` Right (Program [Struct{name = "Teacher", fields = [], refinement = Nothing, refinementSrc = "", is = []}])
it "Can define with is" $ do
parseProgram "struct Teacher = () is Person" parserCompilerFlags
`shouldBe` Right (Program [Struct{name = "Teacher", fields = [], refinement = Nothing, refinementSrc = "", is = ["Person"]}])
it "Can define with multiple is" $ do
parseProgram "struct Teacher = () is Person, Employee" parserCompilerFlags
`shouldBe` Right (Program [Struct{name = "Teacher", fields = [], refinement = Nothing, refinementSrc = "", is = ["Person", "Employee"]}])
describe "Traits" $ do
it "Should parse a trait decleration" $
parseProgram "trait Show = do\nshow :: Self -> String\nend" parserCompilerFlags
Expand Down

0 comments on commit 349d044

Please sign in to comment.