瀏覽代碼

Implement type system adjusment to enable implicit casting

Lucas de Souza 5 年之前
父節點
當前提交
d1e1a4cafd
共有 2 個文件被更改,包括 15 次插入2 次删除
  1. 1 1
      js/typeSystem/multiType.js
  2. 14 1
      js/typeSystem/type.js

+ 1 - 1
js/typeSystem/multiType.js

@@ -28,7 +28,7 @@ export class MultiType extends Type {
     if(another instanceof Type) {
       for (let i = 0; i < this.types.length; i++) {
         const t = this.types[i];
-        if (t.isCompatible(another)) {
+        if (another.isCompatible(t)) {
           return true;
         }
       }

+ 14 - 1
js/typeSystem/type.js

@@ -1,3 +1,7 @@
+import { Config } from "../util/config";
+import { Types } from "./types";
+import { BaseTypes } from "./baseTypes";
+
 export class Type {
 
   constructor(baseType) {
@@ -18,7 +22,16 @@ export class Type {
 
   isCompatible (another) {
     if(another instanceof Type) {
-      return this.baseType.isCompatible(another.baseType);
+      let result = this.baseType.isCompatible(another.baseType);
+      if(result) {
+        return true;
+      } else if(Config.enable_type_casting) {
+        if (this.baseType === BaseTypes.INTEGER || this.baseType === BaseTypes.REAL) {
+          if (another.baseType === BaseTypes.INTEGER || another.baseType === BaseTypes.REAL) {
+            return true;
+          }
+        }
+      }
     }
     return false;
   }