From 711128a098e8e410754e7d50a34f2b2add0d1781 Mon Sep 17 00:00:00 2001 From: Lorenzobattistela Date: Sun, 10 Nov 2024 10:02:50 -0300 Subject: [PATCH 1/7] add trigonometric ops --- src/Kind/Type.hs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Kind/Type.hs b/src/Kind/Type.hs index 39c84cccd..b9e639738 100644 --- a/src/Kind/Type.hs +++ b/src/Kind/Type.hs @@ -126,6 +126,7 @@ data Oper | MOD | EQ | NE | LT | GT | LTE | GTE | AND | OR | XOR | LSH | RSH + | COS | SIN | TAN | ATAN deriving Show -- Telescope From 32a6ceaaa0f491e903f1588e812c71e8477fa01d Mon Sep 17 00:00:00 2001 From: Lorenzobattistela Date: Sun, 10 Nov 2024 10:02:57 -0300 Subject: [PATCH 2/7] utils to check if is unary --- src/Kind/Util.hs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Kind/Util.hs b/src/Kind/Util.hs index 5d0bcf837..31fe7e6da 100644 --- a/src/Kind/Util.hs +++ b/src/Kind/Util.hs @@ -162,6 +162,10 @@ getOpReturnType MUL U64 = U64 getOpReturnType MUL F64 = F64 getOpReturnType DIV U64 = U64 getOpReturnType DIV F64 = F64 +getOpReturnType COS F64 = F64 +getOpReturnType SIN F64 = F64 +getOpReturnType TAN F64 = F64 +getOpReturnType ATAN F64 = F64 getOpReturnType MOD U64 = U64 getOpReturnType EQ _ = U64 getOpReturnType NE _ = U64 @@ -182,3 +186,10 @@ checkValidType typ validTypes dep = foldr (\t acc -> do if isEqual then return True else acc ) (return False) validTypes +isUnary :: Oper -> Bool +isUnary COS = True +isUnary SIN = True +isUnary TAN = True +isUnary ATAN = True +isUnary _ = False + From 1a09933c0dbab149b14cd515581f6d2df4178bed Mon Sep 17 00:00:00 2001 From: Lorenzobattistela Date: Sun, 10 Nov 2024 10:03:02 -0300 Subject: [PATCH 3/7] hiding second operator on parser --- src/Kind/Parse.hs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Kind/Parse.hs b/src/Kind/Parse.hs index 409b5b27c..72d13ed83 100644 --- a/src/Kind/Parse.hs +++ b/src/Kind/Parse.hs @@ -14,6 +14,7 @@ import Kind.Equal import Kind.Reduce import Kind.Show import Kind.Type +import Kind.Util import Prelude hiding (EQ, LT, GT) import System.Console.ANSI import Text.Parsec ((), (<|>), getPosition, sourceLine, sourceColumn, getState, setState) @@ -530,7 +531,9 @@ parseOp2 = withSrc $ do char_skp '(' opr <- parseOper fst <- parseTerm - snd <- parseTerm + snd <- if isUnary opr + then return (Flt 0.0) -- Fill snd with `Flt 0.0` for unary operators + else parseTerm -- Parse the second term for binary operators char ')' return $ Op2 opr fst snd @@ -608,6 +611,10 @@ parseOper = P.choice , P.try (string_skp "&") >> return AND , P.try (string_skp "|") >> return OR , P.try (string_skp "^") >> return XOR + , P.try (string_skp "cos") >> return COS + , P.try (string_skp "sin") >> return SIN + , P.try (string_skp "tan") >> return TAN + , P.try (string_skp "atan") >> return ATAN ] "Binary operator" parseSuffix :: Term -> Parser Term From b1f0a4c2be73c57ccb54ed5aa66552416eda3b17 Mon Sep 17 00:00:00 2001 From: Lorenzobattistela Date: Sun, 10 Nov 2024 10:03:07 -0300 Subject: [PATCH 4/7] reduce and show of trigonometric --- src/Kind/Reduce.hs | 7 +++++++ src/Kind/Show.hs | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/src/Kind/Reduce.hs b/src/Kind/Reduce.hs index ed2a03ae9..a3e79877d 100644 --- a/src/Kind/Reduce.hs +++ b/src/Kind/Reduce.hs @@ -85,6 +85,9 @@ reduce book fill lv term = red term where op2 XOR (Num fst) (Num snd) = Num (fst `xor` snd) op2 LSH (Num fst) (Num snd) = Num (shiftL fst (fromIntegral snd)) op2 RSH (Num fst) (Num snd) = Num (shiftR fst (fromIntegral snd)) + op2 COS (Num _) _ = error "COS operation not supported for integer values" + op2 SIN (Num _) _ = error "SIN operation not supported for integer values" + op2 ATAN (Num _) _ = error "ATAN2 operation not supported for integer values" op2 op (Ref nam) (Flt snd) | lv > 0 = op2 op (ref nam) (Flt snd) op2 op (Flt fst) (Ref nam) | lv > 0 = op2 op (Flt fst) (ref nam) op2 ADD (Flt fst) (Flt snd) = Flt (fst + snd) @@ -101,6 +104,10 @@ reduce book fill lv term = red term where op2 AND (Flt _) (Flt _) = error "Bitwise AND not supported for floating-point numbers" op2 OR (Flt _) (Flt _) = error "Bitwise OR not supported for floating-point numbers" op2 XOR (Flt _) (Flt _) = error "Bitwise XOR not supported for floating-point numbers" + op2 COS (Flt fst) _ = Flt (cos fst) + op2 SIN (Flt fst) _ = Flt (sin fst) + op2 TAN (Flt fst) _ = Flt (tan fst) + op2 ATAN (Flt fst) _ = Flt (atan fst) op2 opr fst snd = Op2 opr fst snd ref nam | lv > 0 = case M.lookup nam book of diff --git a/src/Kind/Show.hs b/src/Kind/Show.hs index 36203d050..d8c65695b 100644 --- a/src/Kind/Show.hs +++ b/src/Kind/Show.hs @@ -174,6 +174,10 @@ showOper OR = "|" showOper XOR = "^" showOper LSH = "<<" showOper RSH = ">>" +showOper COS = "cos" +showOper SIN = "sin" +showOper TAN = "tan" +showOper ATAN = "atan" -- Pretty Printing (Sugars) -- ------------------------ From e045bef10e12cc5803c590b8a905ed42d579a3be Mon Sep 17 00:00:00 2001 From: Lorenzobattistela Date: Sun, 10 Nov 2024 10:17:01 -0300 Subject: [PATCH 5/7] adding atan2 --- src/Kind/Parse.hs | 1 + src/Kind/Reduce.hs | 2 ++ src/Kind/Show.hs | 1 + src/Kind/Type.hs | 11 ++++++----- src/Kind/Util.hs | 3 ++- 5 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Kind/Parse.hs b/src/Kind/Parse.hs index 72d13ed83..ce0cdf1a5 100644 --- a/src/Kind/Parse.hs +++ b/src/Kind/Parse.hs @@ -614,6 +614,7 @@ parseOper = P.choice , P.try (string_skp "cos") >> return COS , P.try (string_skp "sin") >> return SIN , P.try (string_skp "tan") >> return TAN + , P.try (string_skp "atan2") >> return ATAN2 , P.try (string_skp "atan") >> return ATAN ] "Binary operator" diff --git a/src/Kind/Reduce.hs b/src/Kind/Reduce.hs index a3e79877d..1335f879e 100644 --- a/src/Kind/Reduce.hs +++ b/src/Kind/Reduce.hs @@ -88,6 +88,7 @@ reduce book fill lv term = red term where op2 COS (Num _) _ = error "COS operation not supported for integer values" op2 SIN (Num _) _ = error "SIN operation not supported for integer values" op2 ATAN (Num _) _ = error "ATAN2 operation not supported for integer values" + op2 ATAN2 (Num _) (Num _) = error "ATAN2 operation not supported for integer values" op2 op (Ref nam) (Flt snd) | lv > 0 = op2 op (ref nam) (Flt snd) op2 op (Flt fst) (Ref nam) | lv > 0 = op2 op (Flt fst) (ref nam) op2 ADD (Flt fst) (Flt snd) = Flt (fst + snd) @@ -108,6 +109,7 @@ reduce book fill lv term = red term where op2 SIN (Flt fst) _ = Flt (sin fst) op2 TAN (Flt fst) _ = Flt (tan fst) op2 ATAN (Flt fst) _ = Flt (atan fst) + op2 ATAN2 (Flt fst) (Flt snd) = Flt (atan2 fst snd) op2 opr fst snd = Op2 opr fst snd ref nam | lv > 0 = case M.lookup nam book of diff --git a/src/Kind/Show.hs b/src/Kind/Show.hs index d8c65695b..dcda57f5e 100644 --- a/src/Kind/Show.hs +++ b/src/Kind/Show.hs @@ -178,6 +178,7 @@ showOper COS = "cos" showOper SIN = "sin" showOper TAN = "tan" showOper ATAN = "atan" +showOper ATAN2 = "atan2" -- Pretty Printing (Sugars) -- ------------------------ diff --git a/src/Kind/Type.hs b/src/Kind/Type.hs index b9e639738..aadedba3e 100644 --- a/src/Kind/Type.hs +++ b/src/Kind/Type.hs @@ -122,11 +122,12 @@ data Cod = Cod Loc Loc -- Numeric Operators data Oper - = ADD | SUB | MUL | DIV - | MOD | EQ | NE | LT - | GT | LTE | GTE | AND - | OR | XOR | LSH | RSH - | COS | SIN | TAN | ATAN + = ADD | SUB | MUL | DIV + | MOD | EQ | NE | LT + | GT | LTE | GTE | AND + | OR | XOR | LSH | RSH + | COS | SIN | TAN | ATAN + | ATAN2 deriving Show -- Telescope diff --git a/src/Kind/Util.hs b/src/Kind/Util.hs index 31fe7e6da..690425cd3 100644 --- a/src/Kind/Util.hs +++ b/src/Kind/Util.hs @@ -165,7 +165,8 @@ getOpReturnType DIV F64 = F64 getOpReturnType COS F64 = F64 getOpReturnType SIN F64 = F64 getOpReturnType TAN F64 = F64 -getOpReturnType ATAN F64 = F64 +getOpReturnType ATAN F64 = F64 +getOpReturnType ATAN2 F64 = F64 getOpReturnType MOD U64 = U64 getOpReturnType EQ _ = U64 getOpReturnType NE _ = U64 From 769e2c39118a6f2f42f4b35af73945965784a0b0 Mon Sep 17 00:00:00 2001 From: Lorenzobattistela Date: Tue, 12 Nov 2024 06:46:40 -0300 Subject: [PATCH 6/7] add compilation to trigonometric operators in js --- src/Kind/CompileJS.hs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/Kind/CompileJS.hs b/src/Kind/CompileJS.hs index af3651883..08f5c8176 100644 --- a/src/Kind/CompileJS.hs +++ b/src/Kind/CompileJS.hs @@ -545,6 +545,7 @@ fnToJS book fnName ct@(getArguments -> (fnArgs, fnBody)) = do operToJS XOR = "^" operToJS LSH = "<<" operToJS RSH = ">>" + operToJS _ = "" -- Compiles a CType to TS tyToTS :: CT -> Int -> String @@ -750,11 +751,21 @@ fnToJS book fnName ct@(getArguments -> (fnArgs, fnBody)) = do fstStmt <- ctToJS False fstName fst dep sndStmt <- ctToJS False sndName snd dep - - let retExpr = case typ of - CF64 -> concat [fstName, " ", opr', " ", sndName] - CU64 -> concat ["BigInt.asUintN(64, ", fstName, " ", opr', " ", sndName, ")"] + let retExpr = case (typ, opr) of + (CF64, COS) -> concat ["Math.cos(", fstName, ")"] + (CF64, SIN) -> concat ["Math.sin(", fstName, ")"] + (CF64, TAN) -> concat ["Math.tan(", fstName, ")"] + (CF64, ATAN) -> concat ["Math.atan(", fstName, ")"] + (CF64, ATAN2) -> concat ["Math.atan2(", fstName, ", ", sndName, ")"] + (CF64, _) -> concat [fstName, " ", opr', " ", sndName] + (CU64, _) -> concat ["BigInt.asUintN(64, ", fstName, " ", opr', " ", sndName, ")"] _ -> error ("Invalid type for binary operation: " ++ showCT typ dep) + + + --let retExpr = case typ of + --CF64 -> concat [fstName, " ", opr', " ", sndName] + --CU64 -> concat ["BigInt.asUintN(64, ", fstName, " ", opr', " ", sndName, ")"] + --_ -> error ("Invalid type for binary operation: " ++ showCT typ dep) retStmt <- set var retExpr return $ concat [fstStmt, sndStmt, retStmt] From 9c8c3de337eeb87858e9972fbf5dbe22060c97cf Mon Sep 17 00:00:00 2001 From: Lorenzobattistela Date: Fri, 22 Nov 2024 08:28:00 -0300 Subject: [PATCH 7/7] add round operator (rounding to 2 decimals) --- src/Kind/CompileJS.hs | 7 +------ src/Kind/Parse.hs | 1 + src/Kind/Reduce.hs | 2 ++ src/Kind/Show.hs | 1 + src/Kind/Type.hs | 12 ++++++------ src/Kind/Util.hs | 13 +++++++------ 6 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/Kind/CompileJS.hs b/src/Kind/CompileJS.hs index 08f5c8176..5248afbc4 100644 --- a/src/Kind/CompileJS.hs +++ b/src/Kind/CompileJS.hs @@ -757,16 +757,11 @@ fnToJS book fnName ct@(getArguments -> (fnArgs, fnBody)) = do (CF64, TAN) -> concat ["Math.tan(", fstName, ")"] (CF64, ATAN) -> concat ["Math.atan(", fstName, ")"] (CF64, ATAN2) -> concat ["Math.atan2(", fstName, ", ", sndName, ")"] + (CF64, ROUND) -> concat ["(Math.round(", fstName, " * Math.pow(10, 2)) / Math.pow(10, 2))"] (CF64, _) -> concat [fstName, " ", opr', " ", sndName] (CU64, _) -> concat ["BigInt.asUintN(64, ", fstName, " ", opr', " ", sndName, ")"] _ -> error ("Invalid type for binary operation: " ++ showCT typ dep) - - --let retExpr = case typ of - --CF64 -> concat [fstName, " ", opr', " ", sndName] - --CU64 -> concat ["BigInt.asUintN(64, ", fstName, " ", opr', " ", sndName, ")"] - --_ -> error ("Invalid type for binary operation: " ++ showCT typ dep) - retStmt <- set var retExpr return $ concat [fstStmt, sndStmt, retStmt] go (CLog msg nxt) = do diff --git a/src/Kind/Parse.hs b/src/Kind/Parse.hs index ce0cdf1a5..eed23029b 100644 --- a/src/Kind/Parse.hs +++ b/src/Kind/Parse.hs @@ -616,6 +616,7 @@ parseOper = P.choice , P.try (string_skp "tan") >> return TAN , P.try (string_skp "atan2") >> return ATAN2 , P.try (string_skp "atan") >> return ATAN + , P.try (string_skp "round") >> return ROUND ] "Binary operator" parseSuffix :: Term -> Parser Term diff --git a/src/Kind/Reduce.hs b/src/Kind/Reduce.hs index 1335f879e..95669ce8d 100644 --- a/src/Kind/Reduce.hs +++ b/src/Kind/Reduce.hs @@ -89,6 +89,7 @@ reduce book fill lv term = red term where op2 SIN (Num _) _ = error "SIN operation not supported for integer values" op2 ATAN (Num _) _ = error "ATAN2 operation not supported for integer values" op2 ATAN2 (Num _) (Num _) = error "ATAN2 operation not supported for integer values" + op2 ROUND (Num _) _ = error "ROUND operation not supported for integer values" op2 op (Ref nam) (Flt snd) | lv > 0 = op2 op (ref nam) (Flt snd) op2 op (Flt fst) (Ref nam) | lv > 0 = op2 op (Flt fst) (ref nam) op2 ADD (Flt fst) (Flt snd) = Flt (fst + snd) @@ -110,6 +111,7 @@ reduce book fill lv term = red term where op2 TAN (Flt fst) _ = Flt (tan fst) op2 ATAN (Flt fst) _ = Flt (atan fst) op2 ATAN2 (Flt fst) (Flt snd) = Flt (atan2 fst snd) + op2 ROUND (Flt fst) (Flt _) = Flt (fromIntegral (round (fst * 100)) / 100) op2 opr fst snd = Op2 opr fst snd ref nam | lv > 0 = case M.lookup nam book of diff --git a/src/Kind/Show.hs b/src/Kind/Show.hs index dcda57f5e..a03fe05ad 100644 --- a/src/Kind/Show.hs +++ b/src/Kind/Show.hs @@ -179,6 +179,7 @@ showOper SIN = "sin" showOper TAN = "tan" showOper ATAN = "atan" showOper ATAN2 = "atan2" +showOper ROUND = "round" -- Pretty Printing (Sugars) -- ------------------------ diff --git a/src/Kind/Type.hs b/src/Kind/Type.hs index aadedba3e..b4d9ce1b7 100644 --- a/src/Kind/Type.hs +++ b/src/Kind/Type.hs @@ -122,12 +122,12 @@ data Cod = Cod Loc Loc -- Numeric Operators data Oper - = ADD | SUB | MUL | DIV - | MOD | EQ | NE | LT - | GT | LTE | GTE | AND - | OR | XOR | LSH | RSH - | COS | SIN | TAN | ATAN - | ATAN2 + = ADD | SUB | MUL | DIV + | MOD | EQ | NE | LT + | GT | LTE | GTE | AND + | OR | XOR | LSH | RSH + | COS | SIN | TAN | ATAN + | ATAN2 | ROUND deriving Show -- Telescope diff --git a/src/Kind/Util.hs b/src/Kind/Util.hs index 690425cd3..8395043e9 100644 --- a/src/Kind/Util.hs +++ b/src/Kind/Util.hs @@ -167,6 +167,7 @@ getOpReturnType SIN F64 = F64 getOpReturnType TAN F64 = F64 getOpReturnType ATAN F64 = F64 getOpReturnType ATAN2 F64 = F64 +getOpReturnType ROUND F64 = F64 getOpReturnType MOD U64 = U64 getOpReturnType EQ _ = U64 getOpReturnType NE _ = U64 @@ -188,9 +189,9 @@ checkValidType typ validTypes dep = foldr (\t acc -> do ) (return False) validTypes isUnary :: Oper -> Bool -isUnary COS = True -isUnary SIN = True -isUnary TAN = True -isUnary ATAN = True -isUnary _ = False - +isUnary COS = True +isUnary SIN = True +isUnary TAN = True +isUnary ATAN = True +isUnary ROUND = True +isUnary _ = False