Skip to content

Commit

Permalink
Do not force div to use decimal
Browse files Browse the repository at this point in the history
  • Loading branch information
vipentti committed Dec 8, 2023
1 parent 13a05b4 commit f71d7ec
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 33 deletions.
48 changes: 24 additions & 24 deletions src/Visp.Compiler/Syntax/SynWriter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -779,9 +779,16 @@ module Write =
| SynExpr.Symbol name ->
match w.knownMethods.TryFind(Syntax.textOfSymbol name) with
| Some method ->
fmtprintf w "%s.``%s``(" method.DeclaringType.Name method.Name

let mutable parens = false

fmtprintf w "%s.``%s``" method.DeclaringType.Name method.Name

if isVariableArgMethod method then
parens <- true
string w "("


string w "state"

if not args.IsEmpty then
Expand All @@ -795,6 +802,9 @@ module Write =
string wt ")")
args
else if hasSingleValueArrayTypeArg method then
parens <- true
string w "("

writeArgComma
w
(fun wt stt ex ->
Expand All @@ -803,8 +813,15 @@ module Write =
string wt ")")
args
else if hasParamArrayAttribute method then
parens <- true
string w "("

writeSeq w WriteState.Arg (flip string ", ") writeExpr args
else

// TODO: Support
parens <- true
string w "("
let parameters = method.GetParameters()
let zipped = Seq.zip args parameters

Expand All @@ -824,7 +841,8 @@ module Write =

()

char w ')'
if parens then
char w ')'
| None ->
symbol w name true
writeCallArgs w args
Expand Down Expand Up @@ -1270,27 +1288,17 @@ module Write =
startExpr w st r

match args with
| [] -> string w "1"
| [] -> string w "LanguagePrimitives.GenericOne"
| [ one ] -> writeExpr w st one
| rest -> writeSeq w WriteState.Inline (flip string " * ") writeExpr rest
| SynOp.Div(args, r) ->
startExpr w st r

match args with
| [ one ] ->
string w "1.0m / (decimal "
writeExpr w WriteState.Inline one
string w ")"
| rest ->
writeSeq
w
WriteState.Inline
(flip string " / ")
(fun w st a ->
string w "(decimal "
writeExpr w st a
string w ")")
rest
string w "LanguagePrimitives.GenericOne / "
writeExprInParens w WriteState.Inline one
| rest -> writeSeq w WriteState.Inline (flip string " / ") writeExprInParens rest
| SynOp.Minus(args, r) ->
startExpr w st r

Expand All @@ -1309,15 +1317,7 @@ module Write =
string w "(fun "
writeArgsOrEmpty w args
string w " ->"

// if body.Length = 1 then
// space w
// for ex in body do
// writeExpr w wsNone ex
// else
// use _ = withIndent w false
writeBody w writeExpr body

string w ")"
()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,21 @@ printfn ("* is %i") (a * b * 52)
// line 16 @"example-5.visp"
printfn ("* is %i") (2)
// line 17 @"example-5.visp"
printfn ("* is %i") (1)
printfn ("* is %i") (LanguagePrimitives.GenericOne)
// line 19 @"example-5.visp"
printfn ("- is %i") (a - b - 52)
// line 20 @"example-5.visp"
printfn ("- is %i") (-2)
// line 22 @"example-5.visp"
printfn ("/ is %f") ((decimal a) / (decimal b) / (decimal 52))
printfn ("/ is %A") ((a) / (b) / (52))
// line 23 @"example-5.visp"
printfn ("/ is %A") (LanguagePrimitives.GenericOne / (2))
// line 25 @"example-5.visp"
printfn ("/ is %A") ((decimal (a)) / (decimal (b)) / (decimal (52.0)))
// line 26 @"example-5.visp"
let visp_result_todo =
// line 23 @"example-5.visp"
printfn ("/ is %f") (1.0m / (decimal 2))
// line 23 @"example-5.visp"
// line 26 @"example-5.visp"
printfn ("/ is %A") (LanguagePrimitives.GenericOne / (2))
// line 26 @"example-5.visp"
printfn ("%A") (visp_result_todo)

Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
* is 1
- is -51
- is -2
/ is 0.024038
/ is 0.500000
/ is 0
/ is 0
/ is 0.0240384615384615384615384615M
/ is 0
()

ExitCode: 0
7 changes: 5 additions & 2 deletions visp/tests/examples/example-5.visp
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@
(printfn "- is %i" (- a b 52))
(printfn "- is %i" (- 2))

(printfn "/ is %f" (/ a b 52))
(printfn "/ is %f" (/ 2))
(printfn "/ is %A" (/ a b 52))
(printfn "/ is %A" (/ 2))

(printfn "/ is %A" (/ (decimal a) (decimal b) (decimal 52.0)))
(printfn "/ is %A" (/ 2))

0 comments on commit f71d7ec

Please sign in to comment.