mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-11 05:25:28 +03:00
core: rename regex to pattern, as @RyanSquared suggested in #192
This commit is contained in:
parent
5da0e91de8
commit
5c736fd224
10
core/Pattern.carp
Normal file
10
core/Pattern.carp
Normal file
@ -0,0 +1,10 @@
|
||||
(system-include "carp_pattern.h")
|
||||
|
||||
(defmodule Pattern
|
||||
(register find (Fn [&Pattern &String] Int))
|
||||
(register match (Fn [&Pattern &String] (Array String)))
|
||||
(register match-str (Fn [&Pattern &String] String))
|
||||
(register global-match (Fn [&Pattern &String] (Array (Array String))))
|
||||
(register substitute (Fn [&Pattern &String &String Int] String))
|
||||
(defn matches? [pat s] (/= (find pat s) -1))
|
||||
)
|
@ -1,10 +0,0 @@
|
||||
(system-include "carp_regex.h")
|
||||
|
||||
(defmodule Regex
|
||||
(register find (Fn [&Regex &String] Int))
|
||||
(register match (Fn [&Regex &String] (Array String)))
|
||||
(register match-str (Fn [&Regex &String] String))
|
||||
(register global-match (Fn [&Regex &String] (Array (Array String))))
|
||||
(register substitute (Fn [&Regex &String &String Int] String))
|
||||
(defn matches? [pat s] (/= (find pat s) -1))
|
||||
)
|
@ -413,7 +413,7 @@ void reprepstate(MatchState *ms) {
|
||||
assert(ms->matchdepth == MAXCCALLS);
|
||||
}
|
||||
|
||||
int Regex_find(string* p, string* s) {
|
||||
int Pattern_find(string* p, string* s) {
|
||||
string str = *s;
|
||||
string pat = *p;
|
||||
int lstr = strlen(str);
|
||||
@ -440,7 +440,7 @@ int Regex_find(string* p, string* s) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
Array Regex_match(string* p, string* s) {
|
||||
Array Pattern_match(string* p, string* s) {
|
||||
string str = *s;
|
||||
string pat = *p;
|
||||
int lstr = strlen(str);
|
||||
@ -463,7 +463,7 @@ Array Regex_match(string* p, string* s) {
|
||||
return a;
|
||||
}
|
||||
|
||||
string Regex_match_MINUS_str(string* p, string* s) {
|
||||
string Pattern_match_MINUS_str(string* p, string* s) {
|
||||
string str = *s;
|
||||
string pat = *p;
|
||||
int lstr = strlen(str);
|
||||
@ -527,7 +527,7 @@ Array push_back(Array res, Array tmp) {
|
||||
return res;
|
||||
}
|
||||
|
||||
Array Regex_global_MINUS_match(string* p, string* s) {
|
||||
Array Pattern_global_MINUS_match(string* p, string* s) {
|
||||
string str = *s;
|
||||
string pat = *p;
|
||||
int lstr = strlen(str);
|
||||
@ -582,7 +582,7 @@ string add_value(MatchState *ms, string res, string src, string e, string tr) {
|
||||
return res;
|
||||
}
|
||||
|
||||
string Regex_substitute(string* p, string *s, string *t, int ns) {
|
||||
string Pattern_substitute(string* p, string *s, string *t, int ns) {
|
||||
string str = *s;
|
||||
string pat = *p;
|
||||
string tr = *t;
|
@ -24,7 +24,7 @@ To learn more about the details of memory management, check out [Memory.md](http
|
||||
10.0 ;; Double
|
||||
true ;; Bool
|
||||
"hello" ;; &String
|
||||
\#"hello" ;; &Regex
|
||||
\#"hello" ;; &Pattern
|
||||
\e ;; Char
|
||||
[1 2 3] ;; (Array Int)
|
||||
```
|
||||
@ -38,7 +38,7 @@ Float
|
||||
Double
|
||||
Bool
|
||||
String
|
||||
Regex
|
||||
Pattern
|
||||
Char
|
||||
(Array t)
|
||||
(Fn [<arg-type1> <arg-type2> ...] <return-type>) ;; Function type
|
||||
|
@ -82,7 +82,7 @@ toC toCMode root = emitterSrc (execState (visit startingIndent root) (EmitterSta
|
||||
Num _ _ -> error "Can't emit invalid number type."
|
||||
Bol b -> return (if b then "true" else "false")
|
||||
Str _ -> visitString indent xobj
|
||||
Regex _ -> visitString indent xobj
|
||||
Pattern _ -> visitString indent xobj
|
||||
Chr c -> return $ case c of
|
||||
'\t' -> "'\\t'"
|
||||
'\n' -> "'\\n'"
|
||||
@ -125,7 +125,7 @@ toC toCMode root = emitterSrc (execState (visit startingIndent root) (EmitterSta
|
||||
appendToSrc (addIndent indent ++ "string *" ++ varRef ++ " = &" ++ var ++ ";\n")
|
||||
return varRef
|
||||
visitString indent (XObj (Str str) (Just i) _) = visitStr' indent str i
|
||||
visitString indent (XObj (Regex str) (Just i) _) = visitStr' indent str i
|
||||
visitString indent (XObj (Pattern str) (Just i) _) = visitStr' indent str i
|
||||
visitString _ _ = error "Not a string."
|
||||
escapeString [] = ""
|
||||
escapeString ('\"':xs) = "\\\"" ++ escapeString xs
|
||||
|
@ -65,7 +65,7 @@ initialTypes typeEnv rootEnv root = evalState (visit rootEnv root) 0
|
||||
(Num t _) -> return (Right (xobj { ty = Just t }))
|
||||
(Bol _) -> return (Right (xobj { ty = Just BoolTy }))
|
||||
(Str _) -> return (Right (xobj { ty = Just (RefTy StringTy) }))
|
||||
(Regex _) -> return (Right (xobj { ty = Just (RefTy RegexTy) }))
|
||||
(Pattern _) -> return (Right (xobj { ty = Just (RefTy PatternTy) }))
|
||||
(Chr _) -> return (Right (xobj { ty = Just CharTy }))
|
||||
Break -> return (Right (xobj { ty = Just (FuncTy [] UnitTy)}))
|
||||
(Lst _) -> visitList env xobj
|
||||
|
@ -22,7 +22,7 @@ data Obj = Sym SymPath SymbolMode
|
||||
| InterfaceSym String -- refering to an interface. TODO: rename to InterfaceLookupSym?
|
||||
| Num Ty Double
|
||||
| Str String
|
||||
| Regex String
|
||||
| Pattern String
|
||||
| Chr Char
|
||||
| Bol Bool
|
||||
| Lst [XObj]
|
||||
@ -188,7 +188,7 @@ pretty = visit 0
|
||||
Num DoubleTy num -> show num
|
||||
Num _ _ -> error "Invalid number type."
|
||||
Str str -> show str
|
||||
Regex str -> '#' : show str
|
||||
Pattern str -> '#' : show str
|
||||
Chr c -> '\\' : c : ""
|
||||
Sym path _ -> show path
|
||||
MultiSym originalName paths -> originalName ++ "{" ++ joinWithComma (map show paths) ++ "}"
|
||||
@ -419,7 +419,7 @@ xobjToTy (XObj (Sym (SymPath _ "Float") _) _ _) = Just FloatTy
|
||||
xobjToTy (XObj (Sym (SymPath _ "Double") _) _ _) = Just DoubleTy
|
||||
xobjToTy (XObj (Sym (SymPath _ "Long") _) _ _) = Just LongTy
|
||||
xobjToTy (XObj (Sym (SymPath _ "String") _) _ _) = Just StringTy
|
||||
xobjToTy (XObj (Sym (SymPath _ "Regex") _) _ _) = Just RegexTy
|
||||
xobjToTy (XObj (Sym (SymPath _ "Pattern") _) _ _) = Just PatternTy
|
||||
xobjToTy (XObj (Sym (SymPath _ "Char") _) _ _) = Just CharTy
|
||||
xobjToTy (XObj (Sym (SymPath _ "Bool") _) _ _) = Just BoolTy
|
||||
xobjToTy (XObj (Sym (SymPath _ s@(firstLetter:_)) _) _ _) | isLower firstLetter = Just (VarTy s)
|
||||
|
@ -77,14 +77,14 @@ string = do i <- createInfo
|
||||
incColumn (length str + 2)
|
||||
return (XObj (Str str) i Nothing)
|
||||
|
||||
regex :: Parsec.Parsec String ParseState XObj
|
||||
regex = do i <- createInfo
|
||||
_ <- Parsec.char '#'
|
||||
_ <- Parsec.char '"'
|
||||
str <- Parsec.many (Parsec.try escaped <|> Parsec.noneOf ['"'])
|
||||
_ <- Parsec.char '"'
|
||||
incColumn (length str + 2)
|
||||
return (XObj (Regex $ treat str) i Nothing)
|
||||
pattern :: Parsec.Parsec String ParseState XObj
|
||||
pattern = do i <- createInfo
|
||||
_ <- Parsec.char '#'
|
||||
_ <- Parsec.char '"'
|
||||
str <- Parsec.many (Parsec.try escaped <|> Parsec.noneOf ['"'])
|
||||
_ <- Parsec.char '"'
|
||||
incColumn (length str + 2)
|
||||
return (XObj (Pattern $ treat str) i Nothing)
|
||||
-- auto-escaping backslashes
|
||||
where treat :: String -> String
|
||||
treat [] = []
|
||||
@ -161,7 +161,7 @@ symbol = do i <- createInfo
|
||||
name -> return (XObj (Sym (SymPath (init segments) name) Symbol) i Nothing)
|
||||
|
||||
atom :: Parsec.Parsec String ParseState XObj
|
||||
atom = Parsec.choice [number, regex, string, aChar, symbol]
|
||||
atom = Parsec.choice [number, pattern, string, aChar, symbol]
|
||||
|
||||
incColumn :: Int -> Parsec.Parsec String ParseState ()
|
||||
incColumn x = do s <- Parsec.getState
|
||||
|
@ -28,7 +28,7 @@ coreModules carpDir = map (\s -> carpDir ++ "/core/" ++ s ++ ".carp") [ "Interfa
|
||||
, "String"
|
||||
, "IO"
|
||||
, "System"
|
||||
, "Regex"
|
||||
, "Pattern"
|
||||
]
|
||||
|
||||
-- | The array module contains functions for working with the Array type.
|
||||
|
@ -26,7 +26,7 @@ data Ty = IntTy
|
||||
| FloatTy
|
||||
| DoubleTy
|
||||
| StringTy
|
||||
| RegexTy
|
||||
| PatternTy
|
||||
| CharTy
|
||||
| FuncTy [Ty] Ty
|
||||
| VarTy String
|
||||
@ -53,7 +53,7 @@ instance Show Ty where
|
||||
show LongTy = "Long"
|
||||
show BoolTy = "Bool"
|
||||
show StringTy = "String"
|
||||
show RegexTy = "Regex"
|
||||
show PatternTy = "Pattern"
|
||||
show CharTy = "Char"
|
||||
show (FuncTy argTys retTy) = "(" ++ fnOrLambda ++ " [" ++ joinWithComma (map show argTys) ++ "] " ++ show retTy ++ ")"
|
||||
show (VarTy t) = t
|
||||
@ -88,7 +88,7 @@ tyToCManglePtr _ FloatTy = "float"
|
||||
tyToCManglePtr _ DoubleTy = "double"
|
||||
tyToCManglePtr _ LongTy = "long"
|
||||
tyToCManglePtr _ StringTy = "string"
|
||||
tyToCManglePtr _ RegexTy = "string"
|
||||
tyToCManglePtr _ PatternTy = "string"
|
||||
tyToCManglePtr _ CharTy = "char"
|
||||
tyToCManglePtr _ UnitTy = "void"
|
||||
tyToCManglePtr _ (VarTy x) = x
|
||||
|
@ -1,6 +1,6 @@
|
||||
(load "Test.carp")
|
||||
|
||||
(use-all Regex Test)
|
||||
(use-all Pattern Test)
|
||||
|
||||
(defn main []
|
||||
(with-test test
|
Loading…
Reference in New Issue
Block a user