Skip to content

Commit

Permalink
add builtin
Browse files Browse the repository at this point in the history
  • Loading branch information
vmchale committed Apr 13, 2022
1 parent c18fe31 commit fc2d3d5
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Performance improvements
* Fix bug in how fields were split
* `:` works on column literals
* Add `#*` (list length) builtin

# 1.0.0.0

Expand Down
3 changes: 3 additions & 0 deletions man/MANPAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ Regular expressions follow Rust's regex library: https://docs.rs/regex/

**#** Prefix operator: tally (count bytes in string)

**#\*** Prefix operator: list length
: List a -> a

**,** Ternary operator: zip with
: (a -> b -> c) -> Stream a -> Stream b -> Stream c

Expand Down
4 changes: 3 additions & 1 deletion src/Jacinda/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ data BUn = Tally -- length of string field
| Dedup
| CatMaybes
| Negate
| TallyList -- length of vector
deriving (Eq)

instance Pretty BUn where
Expand All @@ -139,6 +140,7 @@ instance Pretty BUn where
pretty Dedup = "~."
pretty CatMaybes = ".?"
pretty Negate = "-."
pretty TallyList = "#*"

-- ternary
data BTer = ZipW
Expand Down Expand Up @@ -380,7 +382,7 @@ instance Eq (E a) where
(==) (IParseCol _ i) (IParseCol _ j) = i == j
(==) (FParseCol _ i) (FParseCol _ j) = i == j
(==) (Field _ i) (Field _ j) = i == j
(==) LastField{} LastField{} = True
(==) LastField{} LastField{} = True
(==) AllColumn{} AllColumn{} = True
(==) AllField{} AllField{} = True
(==) (EApp _ e0 e1) (EApp _ e0' e1') = e0 == e0' && e1 == e1'
Expand Down
5 changes: 5 additions & 0 deletions src/Jacinda/Backend/Normalize.hs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@ eNorm (EApp ty (UBuiltin ty' Tally) e) = do
pure $ case eI of
StrLit _ str -> IntLit tyI (fromIntegral $ BS.length str)
_ -> EApp ty (UBuiltin ty' Tally) eI
eNorm (EApp ty op@(UBuiltin _ TallyList) e) = do
eI <- eNorm e
pure $ case eI of
(Arr _ xs) -> mkI $ fromIntegral $ V.length xs
_ -> EApp ty op eI
eNorm (EApp ty (EApp ty' op@(BBuiltin _ Lt) e) e') = do
eI <- eNorm e
eI' <- eNorm e'
Expand Down
7 changes: 5 additions & 2 deletions src/Jacinda/Backend/TreeWalk.hs
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,12 @@ eEval (ix, line, ctx) = go where
let str = asStr (go e)
re = asRegex (go e')
bss = splitBy re str
in Arr undefined (StrLit undefined <$> bss)
in Arr undefined (mkStr <$> bss)
go (EApp _ (EApp _ (BBuiltin _ Splitc) e) e') =
let str = asStr (go e)
c = the (asStr (go e'))
bss = BS.split c str
in Arr undefined (StrLit undefined <$> V.fromList bss)
in Arr undefined (mkStr <$> V.fromList bss)
go (EApp _ (EApp _ (EApp _ (TBuiltin _ Substr) e0) e1) e2) =
let eI0 = asStr (go e0)
eI1 = asInt (go e1)
Expand Down Expand Up @@ -352,6 +352,9 @@ eEval (ix, line, ctx) = go where
go (Cond _ p e0 e1) =
let p' = asBool (go p)
in if p' then go e0 else go e1
go (EApp _ (UBuiltin _ TallyList) e) =
let xs = asArr (go e)
in mkI $ fromIntegral $ V.length xs
go e = error ("Internal error: " ++ show e)

-- just shove some big number into the renamer and hope it doesn't clash (bad,
Expand Down
3 changes: 3 additions & 0 deletions src/Jacinda/Lexer.x
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ tokens :-
"," { mkSym Comma }
"." { mkSym Dot }
"#" { mkSym TallyTok }
"#*" { mkSym LengthTok }
"[:" { mkSym ConstTok }
"!" { mkSym Exclamation }
":" { mkSym Colon }
Expand Down Expand Up @@ -274,6 +275,7 @@ data Sym = PlusTok
| Comma
| Dot
| TallyTok
| LengthTok
| ConstTok
| LBracePercent
| LBraceBar
Expand Down Expand Up @@ -322,6 +324,7 @@ instance Pretty Sym where
pretty Comma = ","
pretty Dot = "."
pretty TallyTok = "#"
pretty LengthTok = "#*"
pretty Quot = "\""
pretty Caret = "^"
pretty ConstTok = "[:"
Expand Down
2 changes: 2 additions & 0 deletions src/Jacinda/Parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import Prettyprinter (Pretty (pretty), (<+>))
lbracePercent { TokSym $$ LBracePercent }
lbraceBar { TokSym $$ LBraceBar }
tally { TokSym $$ TallyTok }
tallyL { TokSym $$ LengthTok }
const { TokSym $$ ConstTok }
filter { TokSym $$ FilterTok }
exclamation { TokSym $$ Exclamation }
Expand Down Expand Up @@ -257,6 +258,7 @@ E :: { E AlexPosn }
| lanchor sepBy(E, dot) rparen { Anchor $1 (reverse $2) }
| E E { EApp (eLoc $1) $1 $2 }
| tally { UBuiltin $1 Tally }
| tallyL { UBuiltin $1 TallyList }
| const { UBuiltin $1 Const }
| exclamation { UBuiltin $1 Not }
| lsqbracket E rsqbracket { Dfn $1 $2 }
Expand Down
4 changes: 4 additions & 0 deletions src/Jacinda/Ty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,10 @@ tyE0 (UBuiltin _ IParse) = pure $ UBuiltin (tyArr tyStr tyI) IParse
tyE0 (UBuiltin _ FParse) = pure $ UBuiltin (tyArr tyStr tyF) FParse
tyE0 (UBuiltin _ Floor) = pure $ UBuiltin (tyArr tyF tyI) Floor
tyE0 (UBuiltin _ Ceiling) = pure $ UBuiltin (tyArr tyF tyI) Ceiling
tyE0 (UBuiltin _ TallyList) = do
a <- dummyName "a"
let a' = var a
pure $ UBuiltin (tyArr a' tyI) TallyList
tyE0 (UBuiltin l Negate) = do
a <- dummyName "a"
modify (mapClassVars (addC a (IsNum, l)))
Expand Down
3 changes: 2 additions & 1 deletion test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ main = defaultMain $
, testCase "typechecks dfn" (tyFile "test/examples/ab.jac")
, testCase "parses parens" (tyFile "examples/lib.jac")
, testCase "typechecks/parses correctly" (tyFile "test/examples/line.jac")
, testCase "split eval" (evalTo "[x+' '+y]|'' split '01-23-1987' /-/" " 01 23 1987")
, testCase "split eval" (evalTo "[x+' '+y]|> split '01-23-1987' /-/" "01 23 1987")
, testCase "length eval" (evalTo "#*split '01-23-1987' /-/" "3")
, testCase "captureE" (evalTo "'01-23-1987' ~* 3 /(\\d{2})-(\\d{2})-(\\d{4})/" "Some 1987")
, testCase "if...then...else" (evalTo "if #t then 0 else 1" "0")
]
Expand Down

0 comments on commit fc2d3d5

Please sign in to comment.