Verified Commit e3f954c8 authored by Camil Staps's avatar Camil Staps 🚀

Support passing JavaScript reals to Clean

parent 4d8d6003
...@@ -13,6 +13,7 @@ import Text ...@@ -13,6 +13,7 @@ import Text
= JSInt !Int = JSInt !Int
| JSBool !Bool | JSBool !Bool
| JSString !String | JSString !String
| JSReal !Real
| JSVar !String | JSVar !String
| JSNull | JSNull
...@@ -42,6 +43,7 @@ where ...@@ -42,6 +43,7 @@ where
JSInt i -> toString i JSInt i -> toString i
JSBool b -> if b "true" "false" JSBool b -> if b "true" "false"
JSString s -> "'"+++s+++"'" // TODO escape JSString s -> "'"+++s+++"'" // TODO escape
JSReal r -> toString r
JSVar v -> v JSVar v -> v
JSNull -> "null" JSNull -> "null"
...@@ -83,6 +85,7 @@ jsIsNull v = v=:JSNull ...@@ -83,6 +85,7 @@ jsIsNull v = v=:JSNull
jsValToInt :: !JSVal -> Maybe Int jsValToInt :: !JSVal -> Maybe Int
jsValToInt v = case v of jsValToInt v = case v of
JSInt i -> Just i JSInt i -> Just i
JSReal r -> Just (toInt r)
JSString s -> case toInt s of JSString s -> case toInt s of
0 -> if (s=="0") (Just 0) Nothing 0 -> if (s=="0") (Just 0) Nothing
i -> Just i i -> Just i
...@@ -92,6 +95,7 @@ jsValToBool :: !JSVal -> Maybe Bool ...@@ -92,6 +95,7 @@ jsValToBool :: !JSVal -> Maybe Bool
jsValToBool v = case v of jsValToBool v = case v of
JSBool b -> Just b JSBool b -> Just b
JSInt i -> Just (i<>0) JSInt i -> Just (i<>0)
JSReal r -> Just (r<>0.0)
JSString s -> case s of JSString s -> case s of
"true" -> Just True "true" -> Just True
"false" -> Just False "false" -> Just False
...@@ -102,12 +106,13 @@ jsValToString :: !JSVal -> Maybe String ...@@ -102,12 +106,13 @@ jsValToString :: !JSVal -> Maybe String
jsValToString v = case v of jsValToString v = case v of
JSString s -> Just s JSString s -> Just s
JSInt i -> Just (toString i) JSInt i -> Just (toString i)
JSReal r -> Just (toString r)
JSBool b -> Just (if b "true" "false") JSBool b -> Just (if b "true" "false")
_ -> Nothing _ -> Nothing
// TODO add proper support for Reals
jsValToReal :: !JSVal -> Maybe Real jsValToReal :: !JSVal -> Maybe Real
jsValToReal v = case v of jsValToReal v = case v of
JSReal r -> Just r
JSInt i -> Just (toReal i) JSInt i -> Just (toReal i)
JSString s -> Just (toReal s) JSString s -> Just (toReal s)
_ -> Nothing _ -> Nothing
...@@ -127,7 +132,7 @@ jsValToReal` r v = fromMaybe r (jsValToReal v) ...@@ -127,7 +132,7 @@ jsValToReal` r v = fromMaybe r (jsValToReal v)
gToJS{|Int|} i = JSInt i gToJS{|Int|} i = JSInt i
gToJS{|Bool|} b = JSBool b gToJS{|Bool|} b = JSBool b
gToJS{|String|} s = JSString s gToJS{|String|} s = JSString s
gToJS{|Real|} r = JSInt (toInt r) // TODO gToJS{|Real|} r = JSReal r
gToJS{|JSVal|} v = v gToJS{|JSVal|} v = v
gToJS{|Maybe|} fx v = case v of gToJS{|Maybe|} fx v = case v of
Nothing -> JSNull Nothing -> JSNull
...@@ -276,6 +281,8 @@ cast_value_from_js _ = code { ...@@ -276,6 +281,8 @@ cast_value_from_js _ = code {
jmp_true return_bool jmp_true return_bool
eq_desc _STRING_ 0 0 eq_desc _STRING_ 0 0
jmp_true return_string jmp_true return_string
eq_desc REAL 0 0
jmp_true return_real
pushD_a 0 pushD_a 0
pushI 5290 | 661*8+2; DOMNode (bcprelink.c) pushI 5290 | 661*8+2; DOMNode (bcprelink.c)
eqI eqI
...@@ -311,6 +318,11 @@ cast_value_from_js _ = code { ...@@ -311,6 +318,11 @@ cast_value_from_js _ = code {
fill_r e_iTasks.UI.JS.Interface_kJSBool 0 1 0 0 0 fill_r e_iTasks.UI.JS.Interface_kJSBool 0 1 0 0 0
pop_b 1 pop_b 1
jmp return jmp return
:return_real
repl_r_args 0 1
fill_r e_iTasks.UI.JS.Interface_kJSReal 0 1 0 0 0
pop_b 1
jmp return
:return_string :return_string
fill_r e_iTasks.UI.JS.Interface_kJSString 1 0 1 0 0 fill_r e_iTasks.UI.JS.Interface_kJSString 1 0 1 0 0
pop_a 1 pop_a 1
......
...@@ -112,7 +112,13 @@ const abc_interpreter={ ...@@ -112,7 +112,13 @@ const abc_interpreter={
hp+=16; hp+=16;
hp_free-=2; hp_free-=2;
} else { } else {
throw 'Cannot pass non-integral numbers to Clean yet'; // TODO abc_interpreter.memory_array[store_ptrs/4]=hp;
abc_interpreter.memory_array[hp/4]=21*8+2; // REAL
abc_interpreter.memory_array[hp/4+1]=0;
const float_array=new Float64Array(abc_interpreter.memory_array.buffer, hp+8);
float_array[0]=values[i];
hp+=16;
hp_free-=2;
} }
} else if (typeof values[i]=='boolean') { } else if (typeof values[i]=='boolean') {
abc_interpreter.memory_array[store_ptrs/4]=hp; abc_interpreter.memory_array[store_ptrs/4]=hp;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment