Skip to content

Commit

Permalink
feat: improve typed number literals with extra validation and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
aboeglin committed Mar 31, 2024
1 parent 0072dcc commit cbc6824
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 9 deletions.
4 changes: 4 additions & 0 deletions compiler/common/Error/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ data TypeError
| NotAConstructor String
| RecordDuplicateFields [String]
| TestNotValid Type
| ByteOutOfBounds String
| ShortOutOfBounds String
| IntOutOfBounds String
| NegatedByte
deriving (Show, Eq, Ord)


Expand Down
15 changes: 9 additions & 6 deletions compiler/main/Canonicalize/Canonicalize.hs
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,18 @@ instance Canonicalizable Src.Exp Can.Exp where
return $ Can.Canonical area (Can.LNum x)

Src.LByte x -> do
-- when ((read x :: Int) > 255) $
-- throwError $ CompilationError InvalidLhs (Context (Env.envCurrentPath env) area)
when ((read x :: Int) > 2^8 - 1) $
throwError $ CompilationError (ByteOutOfBounds x) (Context (Env.envCurrentPath env) area)
return $ Can.Canonical area (Can.LByte x)

Src.LShort x -> do
-- when ((read x :: Int) > 255) $
-- throwError $ CompilationError InvalidLhs (Context (Env.envCurrentPath env) area)
when ((read x :: Int) > 2^31 - 1) $
throwError $ CompilationError (ShortOutOfBounds x) (Context (Env.envCurrentPath env) area)
return $ Can.Canonical area (Can.LShort x)

Src.LInt x -> do
-- when ((read x :: Int) > 255) $
-- throwError $ CompilationError InvalidLhs (Context (Env.envCurrentPath env) area)
when ((read x :: Integer) > 2^63 - 1) $
throwError $ CompilationError (IntOutOfBounds x) (Context (Env.envCurrentPath env) area)
return $ Can.Canonical area (Can.LInt x)

Src.LFloat x ->
Expand Down Expand Up @@ -142,6 +142,9 @@ instance Canonicalizable Src.Exp Can.Exp where
Src.App fn args ->
buildApp env target area fn args

Src.UnOp (Src.Source area _ (Src.Var "unary-minus")) (Src.Source _ _ (Src.LByte _)) ->
throwError $ CompilationError NegatedByte (Context (Env.envCurrentPath env) area)

Src.UnOp op arg ->
buildApp env target area op [arg]

Expand Down
92 changes: 92 additions & 0 deletions compiler/main/Explain/Format.hs
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,22 @@ createSimpleErrorDiagnostic color _ typeError = case typeError of
"Grammar error\n\n"
<> "Unexpected character"

ByteOutOfBounds x ->
"Byte out of bounds\n\n"
<> "The literal '" <> x <> "_b' is too big, the maximum value for bytes is 255"

ShortOutOfBounds x ->
"Short out of bounds\n\n"
<> "The literal '" <> x <> "_s' is too big, the maximum value for shorts is "<> show (2^31 - 1)

IntOutOfBounds x ->
"Integer out of bounds\n\n"
<> "The literal '" <> x <> "_i' is too big, the maximum value for integers is "<> show (2^63 - 1)

NegatedByte ->
"Negated byte\n\n"
<> "Bytes can't be negated"

UnknownType t ->
"Unknown type\n\n"
<> "The type '" <> t <> "' was not found\n\n"
Expand Down Expand Up @@ -1316,6 +1332,82 @@ createErrorDiagnostic color context typeError = case typeError of
[]
[Diagnose.Hint "Maybe you forgot to import it?", Diagnose.Hint "Maybe you have a typo?"]

ByteOutOfBounds n ->
case context of
Context modulePath (Area (Loc _ startL startC) (Loc _ endL endC)) ->
Diagnose.Err
Nothing
"Byte out of bounds"
[ ( Diagnose.Position (startL, startC) (endL, endC) modulePath
, Diagnose.This $ "The literal '" <> n <> "_b' is too big, the maximum value for bytes is 255"
)
]
[]

NoContext ->
Diagnose.Err
Nothing
"Byte out of bounds"
[]
[]

ShortOutOfBounds n ->
case context of
Context modulePath (Area (Loc _ startL startC) (Loc _ endL endC)) ->
Diagnose.Err
Nothing
"Short out of bounds"
[ ( Diagnose.Position (startL, startC) (endL, endC) modulePath
, Diagnose.This $ "The literal '" <> n <> "_s' is too big, the maximum value for shorts is "<> show (2^31 - 1) <>"."
)
]
[]

NoContext ->
Diagnose.Err
Nothing
"Short out of bounds"
[]
[]

IntOutOfBounds n ->
case context of
Context modulePath (Area (Loc _ startL startC) (Loc _ endL endC)) ->
Diagnose.Err
Nothing
"Integer out of bounds"
[ ( Diagnose.Position (startL, startC) (endL, endC) modulePath
, Diagnose.This $ "The literal '" <> n <> "_s' is too big, the maximum value for integers is "<> show (2^63 - 1) <>"."
)
]
[]

NoContext ->
Diagnose.Err
Nothing
"Integer out of bounds"
[]
[]

NegatedByte ->
case context of
Context modulePath (Area (Loc _ startL startC) (Loc _ endL endC)) ->
Diagnose.Err
Nothing
"Negated byte"
[ ( Diagnose.Position (startL, startC) (endL, endC) modulePath
, Diagnose.This "Bytes can't be negated"
)
]
[]

NoContext ->
Diagnose.Err
Nothing
"Negated byte"
[]
[]

DerivingAliasNotAllowed n ->
case context of
Context modulePath (Area (Loc _ startL startC) (Loc _ endL endC)) ->
Expand Down
9 changes: 9 additions & 0 deletions compiler/main/Format/Format.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,15 @@ expToDoc comments exp =
Source _ _ (LNum n) ->
(Pretty.pretty n, comments')

Source _ _ (LByte n) ->
(Pretty.pretty n <> Pretty.pretty "_b", comments')

Source _ _ (LShort n) ->
(Pretty.pretty n <> Pretty.pretty "_s", comments')

Source _ _ (LInt n) ->
(Pretty.pretty n <> Pretty.pretty "_i", comments')

Source _ _ (LFloat n) ->
(Pretty.pretty n, comments')

Expand Down
2 changes: 1 addition & 1 deletion madlib.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ cabal-version: 2.0
-- see: https://github.com/sol/hpack

name: madlib
version: 0.23.2
version: 0.23.3
description: Please see the README on GitHub at <https://github.com/madlib-lang/madlib#readme>
homepage: https://github.com/madlib-lang/madlib#readme
bug-reports: https://github.com/madlib-lang/madlib/issues
Expand Down
2 changes: 1 addition & 1 deletion package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: madlib
version: 0.23.2
version: 0.23.3
github: "madlib-lang/madlib"
license: BSD3
author: "Arnaud Boeglin, Brekk Bockrath"
Expand Down
2 changes: 1 addition & 1 deletion pkg/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@madlib-lang/madlib",
"version": "0.23.2",
"version": "0.23.3",
"main": "./src/run.js",
"bin": {
"madlib": "src/run.js"
Expand Down

0 comments on commit cbc6824

Please sign in to comment.