Skip to content

Commit 4b9e944

Browse files
committed
Implement type-level string append function
1 parent 4cbd4f5 commit 4b9e944

File tree

10 files changed

+64
-8
lines changed

10 files changed

+64
-8
lines changed

src/Libraries/Base1/Prelude.bs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package Prelude(
77
PrimParam(..), PrimPort(..),
88
Bit, Rules, Module, Integer, Real, String, Char, SizeOf, Id__,
99
PrimAction, ActionValue, Action, ActionValue_, ActionWorld, AVStruct,
10-
TAdd, TSub, TMul, TDiv, TLog, TExp, TMax, TMin,
10+
TAdd, TSub, TMul, TDiv, TLog, TExp, TMax, TMin, TApp,
1111
Nat(..),
1212
IsModule(..), addModuleRules, addRules,
1313

@@ -2772,6 +2772,8 @@ primitive type TExp :: # -> #
27722772
primitive type TMax :: # -> # -> #
27732773
primitive type TMin :: # -> # -> #
27742774

2775+
primitive type TApp :: $ -> $ -> $
2776+
27752777
------------------
27762778

27772779
--- Bit operations

src/comp/CType.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ import PreIds(idArrow, idPrimPair, idPrimUnit, idBit, idString,
7171
import Util(itos)
7272
import ErrorUtil
7373
import Pragma(IfcPragma)
74-
import NumType
74+
import TypeOps
7575
import PVPrint(PVPrint(..))
7676
import FStringCompat
7777

@@ -507,6 +507,10 @@ normTAp (TCon (TyCon op _ _)) (TCon (TyNum x xpos))
507507
| isJust (res) = cTNum (fromJust res) (getPosition op)
508508
where res = opNumT op [x]
509509

510+
normTAp (TAp (TCon (TyCon op _ _)) (TCon (TyStr x xpos))) (TCon (TyStr y ypos))
511+
| isJust (res) = cTStr (fromJust res) (getPosition op)
512+
where res = opStrT op [x, y]
513+
510514
normTAp f a = TAp f a
511515

512516
getTypeKind :: Type -> Maybe Kind

src/comp/ISyntax.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ import IdPrint
9696
import PreIds(idSizeOf, idId, idBind, idReturn, idPack, idUnpack, idMonad, idLiftModule, idBit, idFromInteger)
9797
import Backend
9898
import Prim(PrimOp(..))
99-
import NumType
99+
import TypeOps
100100
import ConTagInfo
101101
import VModInfo(VModInfo, vArgs, vName, VName(..), {- VeriPortProp(..), -}
102102
VArgInfo(..), VFieldInfo(..), isParam, VWireInfo)
@@ -425,6 +425,9 @@ normITAp (ITAp (ITCon op _ _) (ITNum x)) (ITNum y) | isJust (res) =
425425
normITAp (ITCon op _ _) (ITNum x) | isJust (res) =
426426
mkNumConT (fromJust res)
427427
where res = opNumT op [x]
428+
normITAp (ITAp (ITCon op _ _) (ITStr x)) (ITStr y) | isJust (res) =
429+
ITStr (fromJust res)
430+
where res = opStrT op [x, y]
428431

429432
normITAp f@(ITCon op _ _) a | op == idSizeOf && notVar a =
430433
-- trace ("normITAp: " ++ ppReadable (ITAp f a)) $

src/comp/PreIds.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ idPrimSnd = prelude_id_no fsPrimSnd
8181
idPrimPair = prelude_id_no fsPrimPair
8282
idFalse = prelude_id_no fsFalse
8383
idTrue = prelude_id_no fsTrue
84-
idSizeOf, idTAdd, idTSub, idTMul, idTDiv, idTLog, idTExp, idTMax, idTMin :: Id
84+
idSizeOf, idTAdd, idTSub, idTMul, idTDiv, idTLog, idTExp, idTMax, idTMin, idTApp :: Id
8585
idSizeOf = prelude_id_no fsSizeOf
8686
idTAdd = prelude_id_no fsTAdd
8787
idTSub = prelude_id_no fsTSub
@@ -91,6 +91,7 @@ idTLog = prelude_id_no fsTLog
9191
idTExp = prelude_id_no fsTExp
9292
idTMax = prelude_id_no fsTMax
9393
idTMin = prelude_id_no fsTMin
94+
idTApp = prelude_id_no fsTApp
9495
idAction, idPrimAction, idToPrimAction, idFromPrimAction :: Id
9596
idAction = prelude_id_no fsAction
9697
idPrimAction = prelude_id_no fsPrimAction

src/comp/PreStrings.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ fsTLog = mkFString "TLog"
279279
fsTExp = mkFString "TExp"
280280
fsTMax = mkFString "TMax"
281281
fsTMin = mkFString "TMin"
282+
fsTApp = mkFString "TApp"
282283
fsStaticAssert = mkFString "staticAssert"
283284
fsDynamicAssert = mkFString "dynamicAssert"
284285
fsContinuousAssert = mkFString "continuousAssert"

src/comp/Pred.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import Position
2020
import Id
2121
import IdPrint
2222
import Type
23-
import NumType
23+
import TypeOps
2424
import PFPrint
2525
import CSyntax(CExpr)
2626
import CType
@@ -267,6 +267,8 @@ apTFun :: Type -> Id -> [Type] -> Type
267267
apTFun _ i [TCon (TyNum x px), TCon (TyNum y py)] | Just n <- opNumT i [x, y] = TCon (TyNum n p')
268268
where p' = bestPosition px py
269269
apTFun _ i [TCon (TyNum x px)] | Just n <- opNumT i [x] = TCon (TyNum n px)
270+
apTFun _ i [TCon (TyStr x px), TCon (TyStr y py)] | Just s <- opStrT i [x, y] = TCon (TyStr s p')
271+
where p' = bestPosition px py
270272
apTFun t _ as = foldl TAp t as
271273

272274
-----------------------------------------------------------------------------
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
module NumType(opNumT, numOpNames) where
2-
-- common routines for handling numeric types
1+
module TypeOps(opNumT, numOpNames, opStrT, strOpNames) where
2+
-- common routines for handling numeric and string types
33

44
import Id
5-
import PreIds(idTAdd, idTSub, idTMul, idTDiv, idTLog, idTExp, idTMax, idTMin)
5+
import PreIds(idTAdd, idTSub, idTMul, idTDiv, idTLog, idTExp, idTMax, idTMin, idTApp)
66
import Util(divC, log2)
7+
import FStringCompat(FString, concatFString)
78

89
-- do a numeric type operation on a list of arguments
910
-- note that we have to validate that the result is going to
@@ -21,3 +22,10 @@ opNumT _ _ = Nothing
2122

2223
numOpNames :: [Id]
2324
numOpNames = [idTAdd, idTSub, idTMul, idTDiv, idTExp, idTLog, idTMax, idTMin]
25+
26+
opStrT :: Id -> [FString] -> Maybe FString
27+
opStrT i xs | i == idTApp = Just $ concatFString xs
28+
opStrT _ _ = Nothing
29+
30+
strOpNames :: [Id]
31+
strOpNames = [idTApp]
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package TApp where
2+
3+
data (WrapStr :: $ -> *) s = WrapStr
4+
5+
printWrapStr :: WrapStr s -> Action
6+
printWrapStr _ = $display (stringOf s)
7+
8+
a :: WrapStr (TApp "aaa" "bbb")
9+
a = WrapStr
10+
11+
class FlatWrapStr a s | a -> s where {}
12+
13+
instance (FlatWrapStr a s2) => FlatWrapStr (WrapStr s1, a) (TApp s1 (TApp "_" s2)) where {}
14+
instance FlatWrapStr (WrapStr s) s where {}
15+
instance FlatWrapStr () "" where {}
16+
17+
b :: (FlatWrapStr (WrapStr "aaa", WrapStr "bbb", WrapStr "ccc") s) => WrapStr s
18+
b = WrapStr
19+
20+
c :: (FlatWrapStr () s) => WrapStr s
21+
c = WrapStr
22+
23+
sysTApp :: Module Empty
24+
sysTApp = module
25+
26+
rules
27+
when True ==> do
28+
printWrapStr a
29+
printWrapStr b
30+
printWrapStr c
31+
$finish

testsuite/bsc.typechecker/string/string.exp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ test_c_veri StringOf
1515
test_c_veri_bsv StringOfBSV
1616

1717
test_c_veri TypeClassString
18+
test_c_veri TApp
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
aaabbb
2+
aaa_bbb_ccc
3+

0 commit comments

Comments
 (0)