Skip to content

Commit

Permalink
Merge pull request #191 from mumuki/issue#189-chars-and-single-char-s…
Browse files Browse the repository at this point in the history
…trings-should-be-different

Issue#189 chars and single char strings should be different
  • Loading branch information
flbulgarelli authored Jul 16, 2018
2 parents 74b4661 + 2315452 commit 4d7e269
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 8 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ Mulang is three different - but thighly related - things:
+ [`MuObject`](#muobject)
- [Syntax](#syntax-43)
- [JavaScript Example](#javascript-example)
+ [`MuNumber`, `MuBool`, `MuString` and `MuSymbol`](#munumber-mubool-mustring-and-musymbol)
+ [`MuNumber`, `MuBool`, `MuString`, `MuSymbol` and `MuChar`](#munumber-mubool-mustring-musymbol-and-muchar)
- [Syntax](#syntax-44)
- [Ruby Example](#ruby-example-5)
+ [`MuTuple` and `MuList`](#mutuple-and-mulist)
Expand Down Expand Up @@ -1666,9 +1666,9 @@ for (var i = 0; i < 10; i++) {
(Attribute "bar" (MuNumber 2))]))
```

### `MuNumber`, `MuBool`, `MuString` and `MuSymbol`
### `MuNumber`, `MuBool`, `MuString`, `MuSymbol` and `MuChar`

> Generic number, boolean, string and symbol (atoms) literals
> Generic number, boolean, string, symbol (atoms) and char literals
#### Syntax

Expand All @@ -1688,6 +1688,10 @@ for (var i = 0; i < 10; i++) {
(MuSymbol String)
```

```haskell
(MuChar Char)
```

#### Ruby Example

```ruby
Expand Down
6 changes: 6 additions & 0 deletions spec/HaskellSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,9 @@ spec = do

it "parses inline type annotations with restrictions" $ do
hs "x = 1 :: (Num a, Foldable t) => t a" `shouldBe` Variable "x" (TypeCast (MuNumber 1) (SimpleType "t a" ["Num a", "Foldable t"]))

it "parses chars and single char strings differently" $ do
hs "x = \"a\"" `shouldNotBe` hs "x = 'a'"

it "parses chars as MuChars" $ do
hs "x = 'a'" `shouldBe` Variable "x" (MuChar 'a')
2 changes: 1 addition & 1 deletion spec/JavaSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ spec = do
public char hello() { return 'f'; }
}|] `shouldBe` Class "Foo" Nothing (Sequence [
SubroutineSignature "hello" [] "char" [],
(SimpleMethod "hello" [] (Return (MuString "f")))])
(SimpleMethod "hello" [] (Return (MuChar 'f')))])

it "parses Parameters" $ do
run "public class Foo extends Bar { int succ(int y) {} }" `shouldBe` Class "Foo" (Just "Bar") (Sequence [
Expand Down
2 changes: 2 additions & 0 deletions src/Language/Mulang/Ast.hs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ data Expression
-- ^ Generic boolean literal
| MuString String
-- ^ Generic string literal
| MuChar Char
-- ^ Generic char literal
| MuSymbol String
-- ^ Generic symbol/atom literal
| MuTuple [Expression]
Expand Down
4 changes: 3 additions & 1 deletion src/Language/Mulang/Inspector/Generic/Duplication.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ hasCodeDuplication e = hasDuplicates $ map hash $ filter (not . isLightweight)
isLightweight :: Inspection
isLightweight (MuNumber _) = True
isLightweight (MuString _) = True
isLightweight (MuChar _) = True
isLightweight (MuBool _) = True
isLightweight (Reference _) = True
isLightweight Self = True
isLightweight None = True
isLightweight None = True
isLightweight MuNil = True
isLightweight Equal = True
isLightweight (Application _ es) = not $ any isApplication es
Expand Down Expand Up @@ -43,6 +44,7 @@ hash (SimpleFunction _ _ body) = 13 * (37 + hash body)
hash Self = 15
hash (SimpleProcedure _ _ body) = 17 * (37 + hash body)
hash (Sequence es) = 19 * (37 + positionalHash es)
hash (MuChar e) = 23 * H.hash e
hash _ = 1

positionalHash :: [Expression] -> Int
Expand Down
4 changes: 2 additions & 2 deletions src/Language/Mulang/Parsers/Haskell.hs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ mu (HsModule _ _ _ _ decls) = compact (concatMap muDecls decls)
muExp (HsExpTypeSig _ exp (HsQualType cs t)) = TypeCast (muExp exp) (muType t cs)
muExp e = debug e

muLit (HsCharPrim v) = MuString [v]
muLit (HsCharPrim v) = MuChar v
muLit (HsStringPrim v) = MuString v
muLit (HsChar v) = MuString [v]
muLit (HsChar v) = MuChar v
muLit (HsString v) = MuString v
muLit (HsIntPrim v) = MuNumber . fromIntegral $ v
muLit (HsInt v) = MuNumber . fromIntegral $ v
Expand Down
2 changes: 1 addition & 1 deletion src/Language/Mulang/Parsers/Java.hs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ muLhs (NameLhs (Name names)) = ns names
muName (Name names) = Reference . ns $ names

muLit (String s) = MuString s
muLit (Char c) = MuString [c]
muLit (Char c) = MuChar c
muLit (Int i) = MuNumber (fromIntegral i)
muLit (Float d) = MuNumber d
muLit (Double d) = MuNumber d
Expand Down

0 comments on commit 4d7e269

Please sign in to comment.