ソースを参照

Implement the following convertions: char -> int, int -> char

The int -> char convertion is applied to int & 0xFF
Lucas de Souza 4 年 前
コミット
145634b25b
1 ファイル変更25 行追加0 行削除
  1. 25 0
      js/processor/lib/lang.js

+ 25 - 0
js/processor/lib/lang.js

@@ -17,6 +17,7 @@ import { StoreValue } from '../store/value/store_value';
  * cast_int
  * cast_bool
  * cast_string
+ * cast_char
  */
 
 export function createIsRealFun () {
@@ -148,6 +149,12 @@ export function createCastIntFun () {
         sto.mode = Modes.RETURN;
         return sto;
       }
+      case Types.CHAR.ord: {
+        const temp = new StoreValue(Types.INTEGER, value.charCodeAt(0));
+        sto.insertStore("$", temp);
+        sto.mode = Modes.RETURN;
+        return sto;
+      }
       case Types.STRING.ord: {
         const parser = IVProgParser.createParser(value);
         try {
@@ -217,4 +224,22 @@ export function createCastStringFun () {
     [new Commands.FormalParameter(Types.ALL, 'str', false)],
     block);
   return func;
+}
+
+export function createCastCharFun () {
+  const castCharFun = (store, _) => {
+    const valSV = store.applyStore('charCode');
+    // Force the int value to the range [0, 255]
+    const value = valSV.get().toNumber() & 0xFF;
+    const result = String.fromCharCode(value);
+    const temp = new StoreValue(Types.CHAR, result);
+    store.insertStore("$", temp);
+    store.mode = Modes.RETURN;
+    return store;
+  }
+  const block = new Commands.CommandBlock([], [new Commands.SysCall(castCharFun)]);
+  const func = new Commands.Function('$castChar', Types.CHAR,
+    [new Commands.FormalParameter(Types.INTEGER, 'charCode', false)],
+    block);
+  return func;
 }