Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
3203eb9
parser, scanner, and token changes
max-acebal Mar 23, 2023
2b88a79
Added Test Case and changed parser
max-acebal Mar 23, 2023
a53bd40
Top level parser
max-acebal Mar 23, 2023
a3ac3f2
Basic import frontend implemented
max-acebal Mar 30, 2023
604384e
remove failing test
max-acebal Mar 31, 2023
c060250
Broken Test case with new parser design
max-acebal Apr 12, 2023
c42c6ee
add renaming for import targets
max-acebal Apr 12, 2023
4f47ee2
change list name to element
max-acebal Apr 12, 2023
c158765
Merge remote-tracking branch 'origin' into module-impl
max-acebal Apr 12, 2023
7952d61
Fixed syntax and created passing test case!
max-acebal Apr 12, 2023
ab451ef
merge main
max-acebal Apr 12, 2023
72897b2
fix the tests
xiaoqiangheye Apr 12, 2023
0601510
Add pretty printing for dot
max-acebal Apr 13, 2023
91a6a28
Merge branch 'module-impl' of https://github.com/ssm-lang/sslang into…
max-acebal Apr 13, 2023
a759916
Fix names of tests
max-acebal Apr 13, 2023
66df0f9
pretty printer
xiaoqiangheye Apr 13, 2023
9fbd550
fix test
xiaoqiangheye Apr 13, 2023
ed1258c
Start of main body imports
max-acebal Apr 20, 2023
87b98e6
change test
max-acebal Apr 20, 2023
a84b790
Problems with lowering
max-acebal Apr 26, 2023
4cb95a1
rewrite function signature
xiaoqiangheye Apr 26, 2023
c8220f0
extractImportList
xiaoqiangheye Apr 26, 2023
e68151b
add a helper
xiaoqiangheye Apr 26, 2023
146e7af
build the structure of rewrite process
xiaoqiangheye Apr 26, 2023
52e38f7
update
xiaoqiangheye May 3, 2023
2761664
parser
xiaoqiangheye May 4, 2023
db54396
Separated a test case for import expressions
max-acebal May 4, 2023
926e82e
checking top imports on expressions skeleton
max-acebal May 4, 2023
483a649
comment out helper function
max-acebal May 5, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions regression-tests/tests/import-styling-expr.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

14 changes: 14 additions & 0 deletions regression-tests/tests/import-styling-expr.ssl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Foo
import Example.Module as Baz
import Another.Example with {
x,
y as z
}

main cin cout =
let one = Foo.Bar
let two = Baz.a
let three = Another.Example.z

after 1, cout <- 10
wait cout
1 change: 1 addition & 0 deletions regression-tests/tests/import-styling.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

16 changes: 16 additions & 0 deletions regression-tests/tests/import-styling.ssl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Foo

import Foo.Bar

import Foo.Bar.Baz as Baz

import Foo.Bar.Baz with doo, bar

import Foo.Bar.Baz with {
foo,
bar as baz
}

main cin cout =
after 1, cout <- 10
wait cout
27 changes: 26 additions & 1 deletion src/Front/Ast.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,21 @@ data TopDef
| TopType TypeDef -- ^ Define an algebraic data type
| TopCDefs String -- ^ Inlined block of C definitions
| TopExtern ExternDecl -- ^ Declare external symbol for FFI
| TopImport Import -- ^ Top Level import statement
deriving (Eq, Show, Typeable, Data)

data Import =
ImportSym [Identifier]
| ImportAs [Identifier] Identifier
| ImportWith [Identifier] [ImportElement]
deriving (Eq, Show, Typeable, Data)


data ImportElement
= ElementAs Identifier Identifier
| ElementSymbol Identifier
deriving (Eq, Show, Typeable, Data)

-- | Associate a type with a symbol
data ExternDecl = ExternDecl Identifier Typ
deriving (Eq, Show, Typeable, Data)
Expand Down Expand Up @@ -99,6 +112,7 @@ data Expr
| CCall Identifier [Expr]
| Tuple [Expr]
| ListExpr [Expr]
| ImportId [Identifier]
deriving (Eq, Show, Typeable, Data)

{- | An operator region: a flat list of alternating expressions and operators
Expand Down Expand Up @@ -196,6 +210,17 @@ instance Pretty TopDef where
pretty (TopType t ) = pretty t
pretty (TopCDefs ds) = pretty "$$" <> pretty ds <> pretty "$$"
pretty (TopExtern x ) = pretty x
pretty (TopImport i) = pretty i

instance Pretty Import where
pretty (ImportSym is) = hsep (map pretty is)
pretty (ImportAs is i) = hsep (map pretty is) <+> pretty "as" <+> pretty i
pretty (ImportWith is iel) = hsep (map pretty is) <+> pretty "with" <+> pretty iel


instance Pretty ImportElement where
pretty (ElementAs s a) = pretty s <+> pretty "as" <+> pretty a
pretty (ElementSymbol s) = pretty s

instance Pretty ExternDecl where
pretty (ExternDecl i t) = pretty "extern" <+> pretty i <+> colon <+> pretty t
Expand Down Expand Up @@ -293,7 +318,7 @@ instance Pretty Expr where
(hsep $ punctuate bar $ map prettyPatExprTup as)
where
prettyPatExprTup (p, e) = pretty p <+> pretty "=" <+> braces (pretty e)
pretty (ListExpr es) = brackets $ hsep $ punctuate comma $ map pretty es
-- pretty (ListExpr es) = brackets $ hsep $ punctuate comma $ map pretty es
pretty NoExpr = error "Unexpected NoExpr"

instance Pretty Literal where
Expand Down
29 changes: 29 additions & 0 deletions src/Front/Parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ import Data.Bifunctor (first)
'fun' { Token (_, TFun) }
'match' { Token (_, TMatch) }
'extern' { Token (_, TExtern) }
'import' { Token (_, TImport) }
'with' { Token (_, TWith) }
'as' { Token (_, TAs) }
'=' { Token (_, TEq) }
'<-' { Token (_, TLarrow) }
'->' { Token (_, TRarrow) }
Expand All @@ -50,6 +53,7 @@ import Data.Bifunctor (first)
':' { Token (_, TColon) }
';' { Token (_, TSemicolon) }
',' { Token (_, TComma) }
'.' { Token (_, TDot) }
'_' { Token (_, TUnderscore) }
'@' { Token (_, TAt) }
'&' { Token (_, TAmpersand) }
Expand Down Expand Up @@ -92,6 +96,25 @@ topDef --> TopDef
| defType { TopType $1 }
| defCBlock { TopCDefs $1 }
| defExtern { TopExtern $1 }
| defImport { TopImport $1 }

-- | Import Definition
defImport
: 'import' '{' importPath '}' { ImportSym $3 }
| 'import' '{' importPath 'as' id '}' { ImportAs $3 $5 }
| 'import' '{' importPath 'with' '{' elementItems '}' '}' { ImportWith $3 $6}

importPath
: id { [$1] }
| id '.' importPath { $1 : $3 }

elementItem
: id { ElementSymbol $1 }
| id 'as' id { ElementAs $1 $3 }

elementItems
: elementItem { [$1] }
| elementItem ',' elementItems { $1 : $3 }

-- | Algebraic data type definition.
defType --> TypeDef
Expand Down Expand Up @@ -316,6 +339,12 @@ exprAtom
| '(' ')' { Lit LitEvent }
| '[' exprList ']' { ListExpr $2 }
| '(' expr ',' exprTups ')' { Tuple ($2 : $4) }
| id '.' fullids { ImportId ($1 : $3) }

-- fullIds
fullids
: id { [$1] }
| id '.' fullids { $1 : $3 }

-- | List Expression.
exprList
Expand Down
68 changes: 68 additions & 0 deletions src/Front/RewriteImportId.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
module Front.RewriteImportId where
import qualified Common.Compiler as Compiler
import Common.Identifiers
import Data.Bifunctor (first)
import Data.Generics (Data (..), everywhere, mkT)
import qualified Front.Ast as A
import Data.List



-- | Itereate the AST and first Import ASTs and generates a list of imported information
extractImportList :: A.Program -> [A.Import]
extractImportList (A.Program defs) =
foldr extractImportListTopDef [] defs
where extractImportListTopDef (A.TopImport imp) l = imp : l
extractImportListTopDef _ l = l



findRealIdentifierInImportAs :: [A.Import] -> A.Expr -> Maybe [Identifier]
findRealIdentifierInImportAs [] id = Nothing
findRealIdentifierInImportAs ((A.ImportAs ls id):rest) (impor@(A.ImportId iid@([id1, id2])))
| id1 == id = Just (id2:ls)
| otherwise = findRealIdentifierInImportAs rest impor

findRealIdentifierInImportAs ((A.ImportWith ls iel):rest) (impor@(A.ImportId iid@(_:_))) =
if firstn == ls
then let res = findinImportElement laste iel in
case res of
Just e -> Just (ls ++ [e])
_ -> Nothing
else findRealIdentifierInImportAs rest impor
where firstn = take ((length iid) - 1) iid
laste = last iid


findinImportElement element [] = Nothing
findinImportElement element ((A.ElementSymbol id): res)
| id == element = Just id
| otherwise = findinImportElement element res


desugarDef :: (Data a) => a -> a
desugarDef = (everywhere $ mkT $ substIdentifier)


-- | subst A.ImportId to a correct one
substIdentifier :: A.Expr -> A.Expr
substIdentifier (A.ImportId ids) = error ("unimplemented")
substIdentifier e = e

-- first get top import list,

-- importInTopLevel :: [Identifier] -> A.Program -> ()
-- importInTopLevel impIds program =
-- -- Check if imported expression is exactly one element longer and matches on of the top imports
-- if any (\topLevelIds -> length impIds == length topLevelIds + 1 && zip impIds topLevelIds == topLevelIds) topLevelImportIds
-- then ()
-- else error "Import not found in top-level imports"
-- where
-- topLevelImports = extractImportList program
-- topLevelImportIds = map importToList topLevelImports

-- importToList :: A.Import -> [Identifier]
-- importToList (A.ImportSym ids) = ids
-- importToList (A.ImportAs ids _) = ids
-- importToList (A.ImportWith ids _) = ids

7 changes: 6 additions & 1 deletion src/Front/Scanner.x
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ $blank = [\ \t]
@newline = [\n] | [\r][\n] | [\r]
@identifier = [a-zA-Z_] [a-zA-Z0-9_']*

$symbolBase = [\!\#\%\&\+\-\.\<\=\>\?\@\\\^\|\~]
$symbolBase = [\!\#\%\&\+\-\<\=\>\?\@\\\^\|\~]
$symbolLeading = [$symbolBase\*]
$symbolAfterSlash = [$symbolBase\_\:\"\']
$symbolAny = [$symbolBase\_\:\"\'\*]
Expand Down Expand Up @@ -132,13 +132,15 @@ tokens :-
let { layout TLet TDBar }
match { layoutNL TMatch TBar }
loop { layout TLoop TSemicolon }
import { layout TImport TSemicolon }
\= { layout TEq TSemicolon }
\<\- { layout TLarrow TSemicolon }
\=\> { layout TDRarrow TSemicolon}
par { layout TPar TDBar }
wait { layout TWait TDBar }
fun { layoutNL TFun TSemicolon }
type { layoutNL TType TBar }
with { layout TWith TSemicolon }

-- Keywords that just do as they be.
extern { keyword TExtern }
Expand All @@ -150,9 +152,12 @@ tokens :-
\; { keyword TSemicolon }
\: { keyword TColon }
\, { keyword TComma }
\. { keyword TDot }
\_ { keyword TUnderscore }
\@ { keyword TAt }
\& { keyword TAmpersand }
as { keyword TAs }


-- Reserved keywords.
do { reserved TDo }
Expand Down
1 change: 1 addition & 0 deletions src/Front/Scope.hs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ scopeExpr (A.Seq e e') = mapM_ scopeExpr [e, e']
scopeExpr (A.Lit _) = return ()
scopeExpr (A.ListExpr l) = mapM_ scopeExpr l
scopeExpr A.Break = return ()
scopeExpr (A.ImportId _) = return ()
scopeExpr (A.Tuple es) = mapM_ scopeExpr es
scopeExpr (A.OpRegion e o) =
throwError $
Expand Down
8 changes: 8 additions & 0 deletions src/Front/Token.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ data TokenType
| TSemicolon
| TBar
| TComma
| TDot
| TUnderscore
| TAt
| TAmpersand
Expand All @@ -61,6 +62,9 @@ data TokenType
| TRbrace
| TLbracket
| TRbracket
| TAs
| TWith
| TImport
| TInteger Integer
| TString String
| TId Identifier
Expand Down Expand Up @@ -127,6 +131,10 @@ instance Pretty TokenType where
pretty TRbrace = pretty "}"
pretty TLbracket = pretty "["
pretty TRbracket = pretty "]"
pretty TImport = pretty "import"
pretty TAs = pretty "as"
pretty TWith = pretty "with"
pretty TDot = pretty "."
pretty (TInteger i) = pretty $ show i
pretty (TString s) = pretty $ "\"" <> s <> "\""
pretty (TId i) = pretty i
Expand Down
2 changes: 2 additions & 0 deletions src/IR/LowerAst.hs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ lowerExpr (A.Tuple es) =
where
apply_recurse e [] = e
apply_recurse e (x : xs) = apply_recurse (I.App e x untyped) xs
lowerExpr (A.ImportId _) =
Compiler.unexpected "lowerExpr: Import Id Lowering"
lowerExpr (A.ListExpr _) =
Compiler.unexpected "lowerExpr: ListExprs should have already been desugared"

Expand Down
4 changes: 2 additions & 2 deletions test/text/Tests/ScanBlocksSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ spec = do
it "supports line continuations" $ do
let input = [here|
some long line
is continued with more indentation
is continued withe more indentation
but this is not
|]
output =
Expand All @@ -21,7 +21,7 @@ spec = do
, TId "line"
, TId "is"
, TId "continued"
, TId "with"
, TId "withe"
, TId "more"
, TId "indentation"
, TDBar
Expand Down
1 change: 0 additions & 1 deletion test/text/Tests/ScanOperatorsSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ spec = do
t "%"
t "*"
t "+"
t "."
t "/"
t "<"
t "='"
Expand Down