Browse Source

Implement declaration initial value type casting

Lucas de Souza 5 years ago
parent
commit
06a072eef7

+ 16 - 3
js/processor/ivprogProcessor.js

@@ -13,7 +13,7 @@ import * as Expressions from './../ast/expressions/';
 import { StoreObjectArrayAddress } from './store/storeObjectArrayAddress';
 import { StoreObjectArrayAddressRef } from './store/storeObjectArrayAddressRef';
 import { CompoundType } from './../typeSystem/compoundType';
-import { convertToString } from '../typeSystem/parsers';
+import { convertToString, canImplicitTypeCast } from '../typeSystem/parsers';
 import { Config } from '../util/config';
 import Decimal from 'decimal.js';
 import { ProcessorErrorFactory } from './error/processorErrorFactory';
@@ -612,15 +612,28 @@ export class IVProgProcessor {
         return $value.then(vl => {
           let realValue = vl;
           if (vl !== null) {
+            if(!vl.type.isCompatible(cmd.type)) {
+              if(Config.enable_type_casting && canImplicitTypeCast(cmd.type, vl.type)) {
+                if(Types.INTEGER.isCompatible(cmd.type)) {
+                  realValue = new StoreObject(cmd.type, vl.value.trunc());
+                } else {
+                  realValue = new StoreObject(cmd.type, vl.value);
+                }
+              } else {
+                const stringInfo = typeInfo.type.stringInfo();
+                const info = stringInfo[0];
+                return Promise.reject(ProcessorErrorFactory.incompatible_types_full(info.type, info.dim, cmd.sourceInfo));
+              }
+            }
             if(vl instanceof StoreObjectArrayAddress) {
               if(vl.type instanceof CompoundType) {
-                realValue = Object.assign(new StoreObjectArray(null,null,null), vl.refValue);
+                return Promise.reject(new Error("!!!Critical Error: Compatibility check failed, a Type accepts a CompoundType"))
               } else {
                 realValue = Object.assign(new StoreObject(null,null), vl.refValue);
               }
             }
           } else {
-            realValue = new StoreObject(cmd.type,0);
+            realValue = new StoreObject(cmd.type, 0);
           }
           realValue.readOnly = cmd.isConst;
           store.updateStore(cmd.id, realValue);

+ 1 - 1
js/processor/lib/io.js

@@ -7,7 +7,7 @@ export function createOutputFun () {
   const writeFunction = function (store, _) {
     const val = store.applyStore('p1');
     if(val.type.isCompatible(Types.INTEGER)) {
-      this.output.sendOutput(val.value.toString());
+      this.output.sendOutput(val.value.trunc().toString());
     } else if (val.type.isCompatible(Types.REAL)) {
       if (val.value.dp() <= 0) {
         this.output.sendOutput(val.value.toFixed(1));  

+ 6 - 2
js/processor/semantic/semanticAnalyser.js

@@ -8,6 +8,8 @@ import { resultTypeAfterInfixOp, resultTypeAfterUnaryOp } from '../compatibility
 import { Types } from '../../typeSystem/types';
 import { CompoundType } from '../../typeSystem/compoundType';
 import { MultiType } from '../../typeSystem/multiType';
+import { Config } from '../../util/config';
+import { canImplicitTypeCast } from '../../typeSystem/parsers';
 
 export class SemanticAnalyser {
 
@@ -125,12 +127,14 @@ export class SemanticAnalyser {
           throw ProcessorErrorFactory.incompatible_types_full(info.type, info.dim, declaration.sourceInfo);
         }
         this.insertSymbol(declaration.id, {id: declaration.id, type: declaration.type})
-      } else if(!declaration.type.isCompatible(resultType)) {
+      } else if(!declaration.type.isCompatible(resultType) && !Config.enable_type_casting) {
         const stringInfo = declaration.type.stringInfo();
         const info = stringInfo[0];
         throw ProcessorErrorFactory.incompatible_types_full(info.type, info.dim, declaration.sourceInfo);
+      } else if (Config.enable_type_casting && canImplicitTypeCast(declaration.type, resultType)) {
+        this.insertSymbol(declaration.id, {id: declaration.id, type: declaration.type});
       } else {
-        this.insertSymbol(declaration.id, {id: declaration.id, type: declaration.type})
+        this.insertSymbol(declaration.id, {id: declaration.id, type: declaration.type});
       }
     }
   }

+ 9 - 0
js/typeSystem/parsers.js

@@ -47,6 +47,15 @@ function convertBoolToString (bool) {
   }
 }
 
+export function canImplicitTypeCast (castType, sourceType) {
+  if (castType.isCompatible(Types.INTEGER) || castType.isCompatible(Types.REAL)) {
+    if (sourceType.isCompatible(Types.INTEGER) || sourceType.isCompatible(Types.REAL)) {
+      return true;
+    }
+  }
+  return false;
+}
+
 export function convertToString(stoObj, type) {
   switch (type.ord) {
     case Types.INTEGER.ord: