From 520df5bfd05ca8fa6a71fb3f8d388286dfcf40f7 Mon Sep 17 00:00:00 2001 From: Simon Fraser Date: Tue, 3 Jul 2018 12:11:29 +0100 Subject: [PATCH] Issue #683 - Added explicit define statements A ADefStm class is introduced, which defers to ALetStm behaviour, but enables disambiguation between the two. --- .../org/overture/ast/factory/AstFactory.java | 236 +++--------------- core/ast/src/main/resources/overtureII.astv2 | 1 + .../main/resources/overtureII.astv2.tostring | 3 +- .../vdm/VarShadowingRenameCollector.java | 51 ++-- .../codegen/assistant/StmAssistantIR.java | 29 +-- .../codegen/visitor/StmVisitorIR.java | 41 +-- .../interpreter/eval/ExpressionEvaluator.java | 98 +------- .../interpreter/eval/StatementEvaluator.java | 98 ++------ .../statement/StatementExpressionFinder.java | 40 ++- .../utilities/statement/StatementFinder.java | 41 +-- .../parser/syntax/StatementReader.java | 33 +-- .../pog/visitors/PogParamStmVisitor.java | 69 +++-- .../utilities/FreeVariablesChecker.java | 131 +++------- .../visitor/TypeCheckerStmVisitor.java | 95 ++----- 14 files changed, 237 insertions(+), 729 deletions(-) diff --git a/core/ast/src/main/java/org/overture/ast/factory/AstFactory.java b/core/ast/src/main/java/org/overture/ast/factory/AstFactory.java index 8146128230..d4a89d664f 100644 --- a/core/ast/src/main/java/org/overture/ast/factory/AstFactory.java +++ b/core/ast/src/main/java/org/overture/ast/factory/AstFactory.java @@ -21,214 +21,35 @@ */ package org.overture.ast.factory; -import java.io.File; -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; -import java.util.Vector; - import org.overture.ast.assistant.AstAssistantFactory; import org.overture.ast.assistant.IAstAssistantFactory; -import org.overture.ast.definitions.AAssignmentDefinition; -import org.overture.ast.definitions.AClassClassDefinition; -import org.overture.ast.definitions.AClassInvariantDefinition; -import org.overture.ast.definitions.AEqualsDefinition; -import org.overture.ast.definitions.AExplicitFunctionDefinition; -import org.overture.ast.definitions.AExplicitOperationDefinition; -import org.overture.ast.definitions.AExternalDefinition; -import org.overture.ast.definitions.AImplicitFunctionDefinition; -import org.overture.ast.definitions.AImplicitOperationDefinition; -import org.overture.ast.definitions.AImportedDefinition; -import org.overture.ast.definitions.AInheritedDefinition; -import org.overture.ast.definitions.AInstanceVariableDefinition; -import org.overture.ast.definitions.ALocalDefinition; -import org.overture.ast.definitions.AMultiBindListDefinition; -import org.overture.ast.definitions.AMutexSyncDefinition; -import org.overture.ast.definitions.ANamedTraceDefinition; -import org.overture.ast.definitions.APerSyncDefinition; -import org.overture.ast.definitions.APrivateAccess; -import org.overture.ast.definitions.ARenamedDefinition; -import org.overture.ast.definitions.AStateDefinition; -import org.overture.ast.definitions.ASystemClassDefinition; -import org.overture.ast.definitions.AThreadDefinition; -import org.overture.ast.definitions.ATypeDefinition; -import org.overture.ast.definitions.AUntypedDefinition; -import org.overture.ast.definitions.AValueDefinition; -import org.overture.ast.definitions.PAccess; -import org.overture.ast.definitions.PDefinition; -import org.overture.ast.definitions.SClassDefinition; +import org.overture.ast.definitions.*; import org.overture.ast.definitions.relations.AEqRelation; import org.overture.ast.definitions.relations.AOrdRelation; -import org.overture.ast.definitions.traces.AApplyExpressionTraceCoreDefinition; -import org.overture.ast.definitions.traces.ABracketedExpressionTraceCoreDefinition; -import org.overture.ast.definitions.traces.AConcurrentExpressionTraceCoreDefinition; -import org.overture.ast.definitions.traces.ALetBeStBindingTraceDefinition; -import org.overture.ast.definitions.traces.ALetDefBindingTraceDefinition; -import org.overture.ast.definitions.traces.ARepeatTraceDefinition; -import org.overture.ast.definitions.traces.ATraceDefinitionTerm; -import org.overture.ast.definitions.traces.PTraceCoreDefinition; -import org.overture.ast.definitions.traces.PTraceDefinition; +import org.overture.ast.definitions.traces.*; import org.overture.ast.expressions.*; -import org.overture.ast.intf.lex.ILexIdentifierToken; -import org.overture.ast.intf.lex.ILexLocation; -import org.overture.ast.intf.lex.ILexNameToken; -import org.overture.ast.intf.lex.ILexQuoteToken; -import org.overture.ast.intf.lex.ILexStringToken; -import org.overture.ast.intf.lex.ILexToken; -import org.overture.ast.lex.LexBooleanToken; -import org.overture.ast.lex.LexCharacterToken; -import org.overture.ast.lex.LexIdentifierToken; -import org.overture.ast.lex.LexIntegerToken; -import org.overture.ast.lex.LexKeywordToken; -import org.overture.ast.lex.LexLocation; -import org.overture.ast.lex.LexNameList; -import org.overture.ast.lex.LexNameToken; -import org.overture.ast.lex.LexQuoteToken; -import org.overture.ast.lex.LexRealToken; -import org.overture.ast.lex.LexStringToken; -import org.overture.ast.lex.LexToken; -import org.overture.ast.lex.VDMToken; +import org.overture.ast.intf.lex.*; +import org.overture.ast.lex.*; import org.overture.ast.messages.InternalException; -import org.overture.ast.modules.AAllExport; -import org.overture.ast.modules.AAllImport; -import org.overture.ast.modules.AFromModuleImports; -import org.overture.ast.modules.AFunctionExport; -import org.overture.ast.modules.AFunctionValueImport; -import org.overture.ast.modules.AModuleExports; -import org.overture.ast.modules.AModuleImports; -import org.overture.ast.modules.AModuleModules; -import org.overture.ast.modules.AOperationExport; -import org.overture.ast.modules.AOperationValueImport; -import org.overture.ast.modules.ATypeExport; -import org.overture.ast.modules.ATypeImport; -import org.overture.ast.modules.AValueExport; -import org.overture.ast.modules.AValueValueImport; -import org.overture.ast.modules.PExport; -import org.overture.ast.modules.PImport; +import org.overture.ast.modules.*; import org.overture.ast.node.tokens.TAsync; import org.overture.ast.node.tokens.TStatic; -import org.overture.ast.patterns.ABooleanPattern; -import org.overture.ast.patterns.ACharacterPattern; -import org.overture.ast.patterns.AConcatenationPattern; -import org.overture.ast.patterns.ADefPatternBind; -import org.overture.ast.patterns.AExpressionPattern; -import org.overture.ast.patterns.AIdentifierPattern; -import org.overture.ast.patterns.AIgnorePattern; -import org.overture.ast.patterns.AIntegerPattern; -import org.overture.ast.patterns.AMapPattern; -import org.overture.ast.patterns.AMapUnionPattern; -import org.overture.ast.patterns.AMapletPatternMaplet; -import org.overture.ast.patterns.ANamePatternPair; -import org.overture.ast.patterns.ANilPattern; -import org.overture.ast.patterns.AObjectPattern; -import org.overture.ast.patterns.APatternListTypePair; -import org.overture.ast.patterns.APatternTypePair; -import org.overture.ast.patterns.AQuotePattern; -import org.overture.ast.patterns.ARealPattern; -import org.overture.ast.patterns.ARecordPattern; -import org.overture.ast.patterns.ASeqBind; -import org.overture.ast.patterns.ASeqMultipleBind; -import org.overture.ast.patterns.ASeqPattern; -import org.overture.ast.patterns.ASetBind; -import org.overture.ast.patterns.ASetMultipleBind; -import org.overture.ast.patterns.ASetPattern; -import org.overture.ast.patterns.AStringPattern; -import org.overture.ast.patterns.ATuplePattern; -import org.overture.ast.patterns.ATypeBind; -import org.overture.ast.patterns.ATypeMultipleBind; -import org.overture.ast.patterns.AUnionPattern; -import org.overture.ast.patterns.PBind; -import org.overture.ast.patterns.PMultipleBind; -import org.overture.ast.patterns.PPattern; -import org.overture.ast.statements.AAlwaysStm; -import org.overture.ast.statements.AApplyObjectDesignator; -import org.overture.ast.statements.AAssignmentStm; -import org.overture.ast.statements.AAtomicStm; -import org.overture.ast.statements.ABlockSimpleBlockStm; -import org.overture.ast.statements.ACallObjectStm; -import org.overture.ast.statements.ACallStm; -import org.overture.ast.statements.ACaseAlternativeStm; -import org.overture.ast.statements.ACasesStm; -import org.overture.ast.statements.AClassInvariantStm; -import org.overture.ast.statements.ACyclesStm; -import org.overture.ast.statements.ADurationStm; -import org.overture.ast.statements.AElseIfStm; -import org.overture.ast.statements.AErrorCase; -import org.overture.ast.statements.AErrorStm; -import org.overture.ast.statements.AExitStm; -import org.overture.ast.statements.AExternalClause; -import org.overture.ast.statements.AFieldObjectDesignator; -import org.overture.ast.statements.AFieldStateDesignator; -import org.overture.ast.statements.AForAllStm; -import org.overture.ast.statements.AForIndexStm; -import org.overture.ast.statements.AForPatternBindStm; -import org.overture.ast.statements.AIdentifierObjectDesignator; -import org.overture.ast.statements.AIdentifierStateDesignator; -import org.overture.ast.statements.AIfStm; -import org.overture.ast.statements.ALetBeStStm; -import org.overture.ast.statements.ALetStm; -import org.overture.ast.statements.AMapSeqStateDesignator; -import org.overture.ast.statements.ANewObjectDesignator; -import org.overture.ast.statements.ANonDeterministicSimpleBlockStm; -import org.overture.ast.statements.ANotYetSpecifiedStm; -import org.overture.ast.statements.APeriodicStm; -import org.overture.ast.statements.AReturnStm; -import org.overture.ast.statements.ASelfObjectDesignator; -import org.overture.ast.statements.ASkipStm; -import org.overture.ast.statements.ASpecificationStm; -import org.overture.ast.statements.ASporadicStm; -import org.overture.ast.statements.AStartStm; -import org.overture.ast.statements.AStopStm; -import org.overture.ast.statements.ASubclassResponsibilityStm; -import org.overture.ast.statements.ATixeStm; -import org.overture.ast.statements.ATixeStmtAlternative; -import org.overture.ast.statements.ATrapStm; -import org.overture.ast.statements.AWhileStm; -import org.overture.ast.statements.PObjectDesignator; -import org.overture.ast.statements.PStateDesignator; -import org.overture.ast.statements.PStm; +import org.overture.ast.patterns.*; +import org.overture.ast.statements.*; import org.overture.ast.typechecker.ClassDefinitionSettings; import org.overture.ast.typechecker.NameScope; import org.overture.ast.typechecker.Pass; -import org.overture.ast.types.AAccessSpecifierAccessSpecifier; -import org.overture.ast.types.ABooleanBasicType; -import org.overture.ast.types.ABracketType; -import org.overture.ast.types.ACharBasicType; -import org.overture.ast.types.AClassType; -import org.overture.ast.types.AFieldField; -import org.overture.ast.types.AFunctionType; -import org.overture.ast.types.AInMapMapType; -import org.overture.ast.types.AIntNumericBasicType; -import org.overture.ast.types.AMapMapType; -import org.overture.ast.types.ANamedInvariantType; -import org.overture.ast.types.ANatNumericBasicType; -import org.overture.ast.types.ANatOneNumericBasicType; -import org.overture.ast.types.AOperationType; -import org.overture.ast.types.AOptionalType; -import org.overture.ast.types.AParameterType; -import org.overture.ast.types.AProductType; -import org.overture.ast.types.AQuoteType; -import org.overture.ast.types.ARationalNumericBasicType; -import org.overture.ast.types.ARealNumericBasicType; -import org.overture.ast.types.ARecordInvariantType; -import org.overture.ast.types.ASeq1SeqType; -import org.overture.ast.types.ASeqSeqType; -import org.overture.ast.types.ASet1SetType; -import org.overture.ast.types.ASetSetType; -import org.overture.ast.types.ATokenBasicType; -import org.overture.ast.types.AUndefinedType; -import org.overture.ast.types.AUnionType; -import org.overture.ast.types.AUnknownType; -import org.overture.ast.types.AUnresolvedType; -import org.overture.ast.types.AVoidReturnType; -import org.overture.ast.types.AVoidType; -import org.overture.ast.types.PType; -import org.overture.ast.types.SBasicType; -import org.overture.ast.types.SInvariantType; +import org.overture.ast.types.*; import org.overture.ast.util.ClonableFile; import org.overture.ast.util.ClonableString; import org.overture.ast.util.Utils; +import java.io.File; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Vector; + @SuppressWarnings("deprecation") public class AstFactory { @@ -622,7 +443,7 @@ public static AExplicitFunctionDefinition newAExplicitFunctionDefinition( List defsList = new LinkedList(); defsList.add(result); type.getDefinitions().add(result); - + type.setInstantiated(typeParams == null || typeParams.isEmpty() ? null : false); return result; @@ -944,7 +765,7 @@ public static AEqualsDefinition newAEqualsDefinition(ILexLocation location, result.setPattern(null); result.setTypebind(null); - + if (bind instanceof ASetBind) { result.setSetbind((ASetBind) bind); @@ -953,7 +774,7 @@ public static AEqualsDefinition newAEqualsDefinition(ILexLocation location, { result.setSeqbind((ASeqBind) bind); } - + result.setTest(test); return result; @@ -1056,7 +877,7 @@ public static APerSyncDefinition newAPerSyncDefinition( result.setOpname(opname); result.setGuard(guard); - + if(guard != null) { guard.parent(result); @@ -1137,7 +958,7 @@ public static AAccessSpecifierAccessSpecifier newAAccessSpecifierAccessSpecifier { return newAAccessSpecifierAccessSpecifier(access, isStatic, isAsync, false); } - + public static AAccessSpecifierAccessSpecifier newAAccessSpecifierAccessSpecifier( PAccess access, boolean isStatic, boolean isAsync, boolean isPure) { @@ -1933,7 +1754,7 @@ public static ASeqCompSeqExp newASeqCompSeqExp(ILexLocation start, initExpression(result, start); result.setFirst(first); - + if (bind instanceof ASetBind) { result.setSetBind((ASetBind) bind); @@ -1942,7 +1763,7 @@ public static ASeqCompSeqExp newASeqCompSeqExp(ILexLocation start, { result.setSeqBind((ASeqBind) bind); } - + result.setPredicate(predicate); return result; @@ -2232,7 +2053,7 @@ public static AModuleModules newAModuleModules(File file, /** * Generate the default module name. - * + * * @param location * The textual location of the name * @return The default module name. @@ -2514,7 +2335,7 @@ public static AObjectPattern newAObjectPattern(ILexNameToken classname, result.setType(AstFactory.getAUnresolvedType(classname)); return result; } - + public static ANamePatternPair newANamePatternPair(ILexNameToken name, PPattern pattern) { ANamePatternPair pair = new ANamePatternPair(); @@ -2908,6 +2729,17 @@ public static ALetStm newALetStm(ILexLocation token, return result; } + public static ADefStm newADefStm(ILexLocation token, + List localDefs, PStm readStatement) + { + ADefStm result = new ADefStm(); + initStatement(result, token); + + result.setLocalDefs(localDefs); + result.setStatement(readStatement); + return result; + } + public static ALetBeStStm newALetBeStStm(ILexLocation token, PMultipleBind bind, PExp stexp, PStm statement) { diff --git a/core/ast/src/main/resources/overtureII.astv2 b/core/ast/src/main/resources/overtureII.astv2 index 1cb8133238..78d4f497a9 100644 --- a/core/ast/src/main/resources/overtureII.astv2 +++ b/core/ast/src/main/resources/overtureII.astv2 @@ -600,6 +600,7 @@ stm {-> package='org.overture.ast.statements' | {if} [ifExp]:exp [thenStm]:stm [elseIf]:stm.elseIf* [elseStm]:stm | {letBeSt} [bind]:multipleBind [suchThat]:exp [statement]:stm (def):definition.multiBindList | {let} [localDefs]:definition* [statement]:stm + | {def} [localDefs]:definition* [statement]:stm | {notYetSpecified} [opname]:LexNameToken [args]:exp* /* FIXME -- these params are probably useless */ | {return} [expression]:exp | #SimpleBlock diff --git a/core/ast/src/main/resources/overtureII.astv2.tostring b/core/ast/src/main/resources/overtureII.astv2.tostring index a65bc15973..b4eb26df1c 100644 --- a/core/ast/src/main/resources/overtureII.astv2.tostring +++ b/core/ast/src/main/resources/overtureII.astv2.tostring @@ -218,7 +218,8 @@ import org.overture.ast.util.ToStringUtil; //%stm->#LetDef = "let " [localDefs] " in " [statement] //%stm->#LetDef->def = "def " + $Utils.listToString($[localDefs]$)$ + " in " [statement] %stm->Let = "let " + $Utils.listToString($[localDefs]$)$ + " in " [statement] -%stm->notYetSpecified = "is not yet specified" +%stm->Def = "def " + $Utils.listToString($[localDefs]$)$ + " in " [statement] +%stm->notYetSpecified = "is not yet specified" %stm->return = "return" + $($[expression]$ == null ? "" : " (" + $[expression]$ + ")")$ %stm->#SimpleBlock = ""+$ToStringUtil.getSimpleBlockString(this)$ %stm->#SimpleBlock->block = ""+$ToStringUtil.getBlockSimpleBlockString(this)$ diff --git a/core/codegen/platform/src/main/java/org/overture/codegen/analysis/vdm/VarShadowingRenameCollector.java b/core/codegen/platform/src/main/java/org/overture/codegen/analysis/vdm/VarShadowingRenameCollector.java index e52c63a803..bd09c7a1f2 100644 --- a/core/codegen/platform/src/main/java/org/overture/codegen/analysis/vdm/VarShadowingRenameCollector.java +++ b/core/codegen/platform/src/main/java/org/overture/codegen/analysis/vdm/VarShadowingRenameCollector.java @@ -1,23 +1,9 @@ package org.overture.codegen.analysis.vdm; -import java.util.*; - import org.apache.log4j.Logger; import org.overture.ast.analysis.AnalysisException; import org.overture.ast.analysis.DepthFirstAnalysisAdaptor; -import org.overture.ast.definitions.AClassClassDefinition; -import org.overture.ast.definitions.AExplicitFunctionDefinition; -import org.overture.ast.definitions.AExplicitOperationDefinition; -import org.overture.ast.definitions.AInstanceVariableDefinition; -import org.overture.ast.definitions.ANamedTraceDefinition; -import org.overture.ast.definitions.AStateDefinition; -import org.overture.ast.definitions.ASystemClassDefinition; -import org.overture.ast.definitions.ATypeDefinition; -import org.overture.ast.definitions.AValueDefinition; -import org.overture.ast.definitions.PDefinition; -import org.overture.ast.definitions.SClassDefinition; -import org.overture.ast.definitions.SFunctionDefinition; -import org.overture.ast.definitions.SOperationDefinition; +import org.overture.ast.definitions.*; import org.overture.ast.definitions.traces.ALetBeStBindingTraceDefinition; import org.overture.ast.expressions.*; import org.overture.ast.intf.lex.ILexLocation; @@ -29,19 +15,7 @@ import org.overture.ast.patterns.PBind; import org.overture.ast.patterns.PMultipleBind; import org.overture.ast.patterns.PPattern; -import org.overture.ast.statements.ABlockSimpleBlockStm; -import org.overture.ast.statements.ACaseAlternativeStm; -import org.overture.ast.statements.ACasesStm; -import org.overture.ast.statements.AForAllStm; -import org.overture.ast.statements.AForIndexStm; -import org.overture.ast.statements.AForPatternBindStm; -import org.overture.ast.statements.AIdentifierStateDesignator; -import org.overture.ast.statements.ALetBeStStm; -import org.overture.ast.statements.ALetStm; -import org.overture.ast.statements.ATixeStm; -import org.overture.ast.statements.ATixeStmtAlternative; -import org.overture.ast.statements.ATrapStm; -import org.overture.ast.statements.PStm; +import org.overture.ast.statements.*; import org.overture.ast.typechecker.NameScope; import org.overture.ast.types.AFieldField; import org.overture.ast.types.PType; @@ -49,6 +23,8 @@ import org.overture.typechecker.assistant.ITypeCheckerAssistantFactory; import org.overture.typechecker.assistant.definition.SFunctionDefinitionAssistantTC; +import java.util.*; + /** * This analysis is used to compute new names for variables that shadow other variables. A renaming is suggested if a * definition hides another variable or causes a duplicate definition. In addition to renaming the definition itself, @@ -225,6 +201,25 @@ public void caseALetStm(ALetStm node) throws AnalysisException endScope(defInfo); } + @Override + public void caseADefStm(ADefStm node) throws AnalysisException + { + if (!proceed(node)) + { + return; + } + + DefinitionInfo defInfo = new DefinitionInfo(node.getLocalDefs(), af); + + visitDefs(defInfo.getNodeDefs()); + + openScope(defInfo, node); + + node.getStatement().apply(this); + + endScope(defInfo); + } + @Override public void caseALetBeStExp(ALetBeStExp node) throws AnalysisException { diff --git a/core/codegen/platform/src/main/java/org/overture/codegen/assistant/StmAssistantIR.java b/core/codegen/platform/src/main/java/org/overture/codegen/assistant/StmAssistantIR.java index 9160cc5b22..bf5297b3d2 100644 --- a/core/codegen/platform/src/main/java/org/overture/codegen/assistant/StmAssistantIR.java +++ b/core/codegen/platform/src/main/java/org/overture/codegen/assistant/StmAssistantIR.java @@ -21,19 +21,10 @@ */ package org.overture.codegen.assistant; -import java.util.List; - import org.overture.ast.analysis.AnalysisException; import org.overture.ast.definitions.SOperationDefinition; import org.overture.ast.expressions.PExp; -import org.overture.ast.statements.ABlockSimpleBlockStm; -import org.overture.ast.statements.ACaseAlternativeStm; -import org.overture.ast.statements.AElseIfStm; -import org.overture.ast.statements.AForAllStm; -import org.overture.ast.statements.AForIndexStm; -import org.overture.ast.statements.AIfStm; -import org.overture.ast.statements.ALetStm; -import org.overture.ast.statements.PStm; +import org.overture.ast.statements.*; import org.overture.ast.types.AUnionType; import org.overture.ast.types.PType; import org.overture.codegen.ir.IRInfo; @@ -42,16 +33,9 @@ import org.overture.codegen.ir.declarations.ADefaultClassDeclIR; import org.overture.codegen.ir.declarations.AMethodDeclIR; import org.overture.codegen.ir.declarations.AVarDeclIR; -import org.overture.codegen.ir.statements.AAtomicStmIR; -import org.overture.codegen.ir.statements.ABlockStmIR; -import org.overture.codegen.ir.statements.ACaseAltStmStmIR; -import org.overture.codegen.ir.statements.AElseIfStmIR; -import org.overture.codegen.ir.statements.AForAllStmIR; -import org.overture.codegen.ir.statements.AForIndexStmIR; -import org.overture.codegen.ir.statements.AForLoopStmIR; -import org.overture.codegen.ir.statements.AIfStmIR; -import org.overture.codegen.ir.statements.AMetaStmIR; -import org.overture.codegen.ir.statements.ASuperCallStmIR; +import org.overture.codegen.ir.statements.*; + +import java.util.List; public class StmAssistantIR extends AssistantBase { @@ -128,6 +112,11 @@ public boolean isScoped(ALetStm let) return appearsInRightContext(let); } + public boolean isScoped(ADefStm def) + { + return appearsInRightContext(def); + } + private boolean appearsInRightContext(PStm block) { return !(block.parent() instanceof SOperationDefinition) diff --git a/core/codegen/platform/src/main/java/org/overture/codegen/visitor/StmVisitorIR.java b/core/codegen/platform/src/main/java/org/overture/codegen/visitor/StmVisitorIR.java index d9955048da..20b4a6a495 100644 --- a/core/codegen/platform/src/main/java/org/overture/codegen/visitor/StmVisitorIR.java +++ b/core/codegen/platform/src/main/java/org/overture/codegen/visitor/StmVisitorIR.java @@ -21,16 +21,8 @@ */ package org.overture.codegen.visitor; -import java.util.LinkedList; -import java.util.List; - import org.overture.ast.analysis.AnalysisException; -import org.overture.ast.definitions.AAssignmentDefinition; -import org.overture.ast.definitions.AClassInvariantDefinition; -import org.overture.ast.definitions.AExplicitOperationDefinition; -import org.overture.ast.definitions.AInheritedDefinition; -import org.overture.ast.definitions.PDefinition; -import org.overture.ast.definitions.SClassDefinition; +import org.overture.ast.definitions.*; import org.overture.ast.expressions.ASelfExp; import org.overture.ast.expressions.PExp; import org.overture.ast.intf.lex.ILexNameToken; @@ -40,14 +32,7 @@ import org.overture.ast.statements.*; import org.overture.ast.types.PType; import org.overture.ast.types.SSetType; -import org.overture.codegen.ir.IRInfo; -import org.overture.codegen.ir.SExpIR; -import org.overture.codegen.ir.SMultipleBindIR; -import org.overture.codegen.ir.SObjectDesignatorIR; -import org.overture.codegen.ir.SPatternIR; -import org.overture.codegen.ir.SStateDesignatorIR; -import org.overture.codegen.ir.SStmIR; -import org.overture.codegen.ir.STypeIR; +import org.overture.codegen.ir.*; import org.overture.codegen.ir.declarations.AVarDeclIR; import org.overture.codegen.ir.expressions.AAndBoolBinaryExpIR; import org.overture.codegen.ir.expressions.AReverseUnaryExpIR; @@ -59,6 +44,9 @@ import org.overture.codegen.ir.utils.AHeaderLetBeStIR; import org.overture.config.Settings; +import java.util.LinkedList; +import java.util.List; + public class StmVisitorIR extends AbstractVisitorIR { public StmVisitorIR() @@ -384,6 +372,25 @@ public SStmIR caseALetStm(ALetStm node, IRInfo question) return block; } + @Override + public SStmIR caseADefStm(ADefStm node, IRInfo question) + throws AnalysisException + { + ABlockStmIR block = new ABlockStmIR(); + block.setScoped(question.getStmAssistant().isScoped(node)); + + question.getDeclAssistant().setFinalLocalDefs(node.getLocalDefs(), block.getLocalDefs(), question); + + SStmIR stm = node.getStatement().apply(question.getStmVisitor(), question); + + if (stm != null) + { + block.getStatements().add(stm); + } + + return block; + } + @Override public SStmIR caseAReturnStm(AReturnStm node, IRInfo question) throws AnalysisException diff --git a/core/interpreter/src/main/java/org/overture/interpreter/eval/ExpressionEvaluator.java b/core/interpreter/src/main/java/org/overture/interpreter/eval/ExpressionEvaluator.java index 4d9a2aea59..ae85d518dd 100644 --- a/core/interpreter/src/main/java/org/overture/interpreter/eval/ExpressionEvaluator.java +++ b/core/interpreter/src/main/java/org/overture/interpreter/eval/ExpressionEvaluator.java @@ -1,12 +1,5 @@ package org.overture.interpreter.eval; -import java.util.Collections; -import java.util.HashMap; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - import org.overture.ast.analysis.AnalysisException; import org.overture.ast.analysis.intf.IQuestionAnswer; import org.overture.ast.assistant.pattern.PTypeList; @@ -14,57 +7,7 @@ import org.overture.ast.definitions.AImplicitFunctionDefinition; import org.overture.ast.definitions.AMultiBindListDefinition; import org.overture.ast.definitions.PDefinition; -import org.overture.ast.expressions.AApplyExp; -import org.overture.ast.expressions.ACaseAlternative; -import org.overture.ast.expressions.ACasesExp; -import org.overture.ast.expressions.ADefExp; -import org.overture.ast.expressions.AElseIfExp; -import org.overture.ast.expressions.AExists1Exp; -import org.overture.ast.expressions.AExistsExp; -import org.overture.ast.expressions.AFieldExp; -import org.overture.ast.expressions.AFieldNumberExp; -import org.overture.ast.expressions.AForAllExp; -import org.overture.ast.expressions.AFuncInstatiationExp; -import org.overture.ast.expressions.AHistoryExp; -import org.overture.ast.expressions.AIfExp; -import org.overture.ast.expressions.AIotaExp; -import org.overture.ast.expressions.AIsExp; -import org.overture.ast.expressions.AIsOfBaseClassExp; -import org.overture.ast.expressions.AIsOfClassExp; -import org.overture.ast.expressions.ALambdaExp; -import org.overture.ast.expressions.ALetBeStExp; -import org.overture.ast.expressions.ALetDefExp; -import org.overture.ast.expressions.AMapCompMapExp; -import org.overture.ast.expressions.AMapEnumMapExp; -import org.overture.ast.expressions.AMapletExp; -import org.overture.ast.expressions.AMkBasicExp; -import org.overture.ast.expressions.AMkTypeExp; -import org.overture.ast.expressions.AMuExp; -import org.overture.ast.expressions.ANarrowExp; -import org.overture.ast.expressions.ANewExp; -import org.overture.ast.expressions.ANilExp; -import org.overture.ast.expressions.ANotYetSpecifiedExp; -import org.overture.ast.expressions.APostOpExp; -import org.overture.ast.expressions.APreExp; -import org.overture.ast.expressions.APreOpExp; -import org.overture.ast.expressions.ARecordModifier; -import org.overture.ast.expressions.ASameBaseClassExp; -import org.overture.ast.expressions.ASameClassExp; -import org.overture.ast.expressions.ASelfExp; -import org.overture.ast.expressions.ASeqCompSeqExp; -import org.overture.ast.expressions.ASeqEnumSeqExp; -import org.overture.ast.expressions.ASetCompSetExp; -import org.overture.ast.expressions.ASetEnumSetExp; -import org.overture.ast.expressions.ASetRangeSetExp; -import org.overture.ast.expressions.AStateInitExp; -import org.overture.ast.expressions.ASubclassResponsibilityExp; -import org.overture.ast.expressions.ASubseqExp; -import org.overture.ast.expressions.AThreadIdExp; -import org.overture.ast.expressions.ATimeExp; -import org.overture.ast.expressions.ATupleExp; -import org.overture.ast.expressions.AUndefinedExp; -import org.overture.ast.expressions.AVariableExp; -import org.overture.ast.expressions.PExp; +import org.overture.ast.expressions.*; import org.overture.ast.intf.lex.ILexLocation; import org.overture.ast.intf.lex.ILexNameToken; import org.overture.ast.lex.Dialect; @@ -81,45 +24,14 @@ import org.overture.config.Settings; import org.overture.interpreter.assistant.IInterpreterAssistantFactory; import org.overture.interpreter.debug.BreakpointManager; -import org.overture.interpreter.runtime.Breakpoint; -import org.overture.interpreter.runtime.ClassContext; -import org.overture.interpreter.runtime.Context; -import org.overture.interpreter.runtime.ContextException; -import org.overture.interpreter.runtime.ObjectContext; -import org.overture.interpreter.runtime.PatternMatchException; -import org.overture.interpreter.runtime.ValueException; -import org.overture.interpreter.runtime.VdmRuntime; -import org.overture.interpreter.runtime.VdmRuntimeError; +import org.overture.interpreter.runtime.*; import org.overture.interpreter.scheduler.SharedStateListner; import org.overture.interpreter.scheduler.SystemClock; -import org.overture.interpreter.values.BooleanValue; -import org.overture.interpreter.values.CompFunctionValue; -import org.overture.interpreter.values.FieldMap; -import org.overture.interpreter.values.FunctionValue; -import org.overture.interpreter.values.IntegerValue; -import org.overture.interpreter.values.IterFunctionValue; -import org.overture.interpreter.values.MapValue; -import org.overture.interpreter.values.NameValuePair; -import org.overture.interpreter.values.NameValuePairList; -import org.overture.interpreter.values.NaturalValue; -import org.overture.interpreter.values.NilValue; -import org.overture.interpreter.values.ObjectValue; -import org.overture.interpreter.values.OperationValue; -import org.overture.interpreter.values.Quantifier; -import org.overture.interpreter.values.QuantifierList; -import org.overture.interpreter.values.RecordValue; -import org.overture.interpreter.values.SeqValue; -import org.overture.interpreter.values.SetValue; -import org.overture.interpreter.values.TokenValue; -import org.overture.interpreter.values.TupleValue; -import org.overture.interpreter.values.UndefinedValue; -import org.overture.interpreter.values.Value; -import org.overture.interpreter.values.ValueList; -import org.overture.interpreter.values.ValueMap; -import org.overture.interpreter.values.ValueSet; -import org.overture.interpreter.values.VoidValue; +import org.overture.interpreter.values.*; import org.overture.typechecker.assistant.pattern.PatternListTC; +import java.util.*; + public class ExpressionEvaluator extends BinaryExpressionEvaluator { diff --git a/core/interpreter/src/main/java/org/overture/interpreter/eval/StatementEvaluator.java b/core/interpreter/src/main/java/org/overture/interpreter/eval/StatementEvaluator.java index e73bd052ae..cc53a6c8de 100644 --- a/core/interpreter/src/main/java/org/overture/interpreter/eval/StatementEvaluator.java +++ b/core/interpreter/src/main/java/org/overture/interpreter/eval/StatementEvaluator.java @@ -1,9 +1,5 @@ package org.overture.interpreter.eval; -import java.util.List; -import java.util.ListIterator; -import java.util.Vector; - import org.overture.ast.analysis.AnalysisException; import org.overture.ast.definitions.AClassInvariantDefinition; import org.overture.ast.definitions.PDefinition; @@ -14,50 +10,7 @@ import org.overture.ast.patterns.ASeqBind; import org.overture.ast.patterns.ASetBind; import org.overture.ast.patterns.ATypeBind; -import org.overture.ast.statements.AAlwaysStm; -import org.overture.ast.statements.AApplyObjectDesignator; -import org.overture.ast.statements.AAssignmentStm; -import org.overture.ast.statements.AAtomicStm; -import org.overture.ast.statements.ABlockSimpleBlockStm; -import org.overture.ast.statements.ACallObjectStm; -import org.overture.ast.statements.ACallStm; -import org.overture.ast.statements.ACaseAlternativeStm; -import org.overture.ast.statements.ACasesStm; -import org.overture.ast.statements.AClassInvariantStm; -import org.overture.ast.statements.ACyclesStm; -import org.overture.ast.statements.ADurationStm; -import org.overture.ast.statements.AElseIfStm; -import org.overture.ast.statements.AErrorStm; -import org.overture.ast.statements.AExitStm; -import org.overture.ast.statements.AFieldObjectDesignator; -import org.overture.ast.statements.AFieldStateDesignator; -import org.overture.ast.statements.AForAllStm; -import org.overture.ast.statements.AForIndexStm; -import org.overture.ast.statements.AForPatternBindStm; -import org.overture.ast.statements.AIdentifierObjectDesignator; -import org.overture.ast.statements.AIdentifierStateDesignator; -import org.overture.ast.statements.AIfStm; -import org.overture.ast.statements.ALetBeStStm; -import org.overture.ast.statements.ALetStm; -import org.overture.ast.statements.AMapSeqStateDesignator; -import org.overture.ast.statements.ANewObjectDesignator; -import org.overture.ast.statements.ANonDeterministicSimpleBlockStm; -import org.overture.ast.statements.ANotYetSpecifiedStm; -import org.overture.ast.statements.APeriodicStm; -import org.overture.ast.statements.AReturnStm; -import org.overture.ast.statements.ASelfObjectDesignator; -import org.overture.ast.statements.ASkipStm; -import org.overture.ast.statements.ASpecificationStm; -import org.overture.ast.statements.ASporadicStm; -import org.overture.ast.statements.AStartStm; -import org.overture.ast.statements.AStopStm; -import org.overture.ast.statements.ASubclassResponsibilityStm; -import org.overture.ast.statements.ATixeStm; -import org.overture.ast.statements.ATixeStmtAlternative; -import org.overture.ast.statements.ATrapStm; -import org.overture.ast.statements.AWhileStm; -import org.overture.ast.statements.PStm; -import org.overture.ast.statements.SSimpleBlockStm; +import org.overture.ast.statements.*; import org.overture.ast.types.AUnionType; import org.overture.ast.types.PType; import org.overture.config.Release; @@ -65,42 +18,15 @@ import org.overture.interpreter.debug.BreakpointManager; import org.overture.interpreter.messages.rtlog.RTExtendedTextMessage; import org.overture.interpreter.messages.rtlog.RTLogger; -import org.overture.interpreter.runtime.Breakpoint; -import org.overture.interpreter.runtime.ClassInterpreter; -import org.overture.interpreter.runtime.Context; -import org.overture.interpreter.runtime.ContextException; -import org.overture.interpreter.runtime.ExitException; -import org.overture.interpreter.runtime.ObjectContext; -import org.overture.interpreter.runtime.PatternMatchException; -import org.overture.interpreter.runtime.RootContext; -import org.overture.interpreter.runtime.ValueException; -import org.overture.interpreter.runtime.VdmRuntime; -import org.overture.interpreter.runtime.VdmRuntimeError; -import org.overture.interpreter.scheduler.BasicSchedulableThread; -import org.overture.interpreter.scheduler.ISchedulableThread; -import org.overture.interpreter.scheduler.ObjectThread; -import org.overture.interpreter.scheduler.PeriodicThread; -import org.overture.interpreter.scheduler.SharedStateListner; -import org.overture.interpreter.values.BooleanValue; -import org.overture.interpreter.values.FunctionValue; -import org.overture.interpreter.values.IntegerValue; -import org.overture.interpreter.values.MapValue; -import org.overture.interpreter.values.ObjectValue; -import org.overture.interpreter.values.OperationValue; -import org.overture.interpreter.values.RecordValue; -import org.overture.interpreter.values.SeqValue; -import org.overture.interpreter.values.SetValue; -import org.overture.interpreter.values.UndefinedValue; -import org.overture.interpreter.values.UpdatableValue; -import org.overture.interpreter.values.Value; -import org.overture.interpreter.values.ValueList; -import org.overture.interpreter.values.ValueListenerList; -import org.overture.interpreter.values.ValueMap; -import org.overture.interpreter.values.ValueSet; -import org.overture.interpreter.values.VoidReturnValue; -import org.overture.interpreter.values.VoidValue; +import org.overture.interpreter.runtime.*; +import org.overture.interpreter.scheduler.*; +import org.overture.interpreter.values.*; import org.overture.parser.config.Properties; +import java.util.List; +import java.util.ListIterator; +import java.util.Vector; + public class StatementEvaluator extends DelegateExpressionEvaluator { @@ -809,6 +735,14 @@ public Value caseALetStm(ALetStm node, Context ctxt) return evalLet(node, node.getLocation(), node.getLocalDefs(), node.getStatement(), "statement", ctxt); } + @Override + public Value caseADefStm(ADefStm node, Context ctxt) + throws AnalysisException + { + // rather than duplicate - just delegate to let evaluation + return evalLet(node, node.getLocation(), node.getLocalDefs(), node.getStatement(), "statement", ctxt); + } + @Override public Value caseANotYetSpecifiedStm(ANotYetSpecifiedStm node, Context ctxt) throws AnalysisException diff --git a/core/interpreter/src/main/java/org/overture/interpreter/utilities/statement/StatementExpressionFinder.java b/core/interpreter/src/main/java/org/overture/interpreter/utilities/statement/StatementExpressionFinder.java index e2d30cf40f..9812356a55 100644 --- a/core/interpreter/src/main/java/org/overture/interpreter/utilities/statement/StatementExpressionFinder.java +++ b/core/interpreter/src/main/java/org/overture/interpreter/utilities/statement/StatementExpressionFinder.java @@ -4,32 +4,7 @@ import org.overture.ast.analysis.QuestionAnswerAdaptor; import org.overture.ast.expressions.PExp; import org.overture.ast.node.INode; -import org.overture.ast.statements.AAlwaysStm; -import org.overture.ast.statements.AAssignmentStm; -import org.overture.ast.statements.AAtomicStm; -import org.overture.ast.statements.ACallObjectStm; -import org.overture.ast.statements.ACallStm; -import org.overture.ast.statements.ACaseAlternativeStm; -import org.overture.ast.statements.ACasesStm; -import org.overture.ast.statements.ACyclesStm; -import org.overture.ast.statements.ADurationStm; -import org.overture.ast.statements.AElseIfStm; -import org.overture.ast.statements.AExitStm; -import org.overture.ast.statements.AForAllStm; -import org.overture.ast.statements.AForIndexStm; -import org.overture.ast.statements.AForPatternBindStm; -import org.overture.ast.statements.AIfStm; -import org.overture.ast.statements.ALetBeStStm; -import org.overture.ast.statements.ALetStm; -import org.overture.ast.statements.AReturnStm; -import org.overture.ast.statements.AStartStm; -import org.overture.ast.statements.AStopStm; -import org.overture.ast.statements.ATixeStm; -import org.overture.ast.statements.ATixeStmtAlternative; -import org.overture.ast.statements.ATrapStm; -import org.overture.ast.statements.AWhileStm; -import org.overture.ast.statements.PStm; -import org.overture.ast.statements.SSimpleBlockStm; +import org.overture.ast.statements.*; import org.overture.interpreter.assistant.IInterpreterAssistantFactory; /*************************************** @@ -251,6 +226,19 @@ public PExp caseALetStm(ALetStm stm, Integer lineno) return stm.getStatement().apply(THIS, lineno); } + @Override + public PExp caseADefStm(ADefStm stm, Integer lineno) + throws AnalysisException + { + PExp found = af.createPDefinitionListAssistant().findExpression(stm.getLocalDefs(), lineno); + if (found != null) + { + return found; + } + + return stm.getStatement().apply(THIS, lineno); + } + @Override public PExp caseAReturnStm(AReturnStm stm, Integer lineno) throws AnalysisException diff --git a/core/interpreter/src/main/java/org/overture/interpreter/utilities/statement/StatementFinder.java b/core/interpreter/src/main/java/org/overture/interpreter/utilities/statement/StatementFinder.java index e916d8af1b..4887b220ab 100644 --- a/core/interpreter/src/main/java/org/overture/interpreter/utilities/statement/StatementFinder.java +++ b/core/interpreter/src/main/java/org/overture/interpreter/utilities/statement/StatementFinder.java @@ -3,26 +3,7 @@ import org.overture.ast.analysis.AnalysisException; import org.overture.ast.analysis.QuestionAnswerAdaptor; import org.overture.ast.node.INode; -import org.overture.ast.statements.AAlwaysStm; -import org.overture.ast.statements.AAssignmentStm; -import org.overture.ast.statements.AAtomicStm; -import org.overture.ast.statements.ACaseAlternativeStm; -import org.overture.ast.statements.ACasesStm; -import org.overture.ast.statements.ACyclesStm; -import org.overture.ast.statements.ADurationStm; -import org.overture.ast.statements.AElseIfStm; -import org.overture.ast.statements.AForAllStm; -import org.overture.ast.statements.AForIndexStm; -import org.overture.ast.statements.AForPatternBindStm; -import org.overture.ast.statements.AIfStm; -import org.overture.ast.statements.ALetBeStStm; -import org.overture.ast.statements.ALetStm; -import org.overture.ast.statements.ATixeStm; -import org.overture.ast.statements.ATixeStmtAlternative; -import org.overture.ast.statements.ATrapStm; -import org.overture.ast.statements.AWhileStm; -import org.overture.ast.statements.PStm; -import org.overture.ast.statements.SSimpleBlockStm; +import org.overture.ast.statements.*; import org.overture.interpreter.assistant.IInterpreterAssistantFactory; /*************************************** @@ -233,6 +214,26 @@ public PStm caseALetStm(ALetStm stm, Integer lineno) // lineno); } + @Override + public PStm caseADefStm(ADefStm stm, Integer lineno) + throws AnalysisException + { + PStm found = findStatementBaseCase(stm, lineno); + if (found != null) + { + return found; + } + + found = af.createPDefinitionAssistant().findStatement(stm.getLocalDefs(), lineno); + if (found != null) + { + return found; + } + + return stm.getStatement().apply(THIS, lineno); // StmAssistantInterpreter.findStatement(stm.getStatement(), + // lineno); + } + @Override public PStm defaultSSimpleBlockStm(SSimpleBlockStm stm, Integer lineno) throws AnalysisException diff --git a/core/parser/src/main/java/org/overture/parser/syntax/StatementReader.java b/core/parser/src/main/java/org/overture/parser/syntax/StatementReader.java index 54e6fbd551..7dac5b5428 100644 --- a/core/parser/src/main/java/org/overture/parser/syntax/StatementReader.java +++ b/core/parser/src/main/java/org/overture/parser/syntax/StatementReader.java @@ -23,38 +23,16 @@ package org.overture.parser.syntax; -import java.util.List; -import java.util.Vector; - import org.overture.ast.definitions.AAssignmentDefinition; import org.overture.ast.definitions.PDefinition; import org.overture.ast.expressions.PExp; import org.overture.ast.factory.AstFactory; import org.overture.ast.intf.lex.ILexLocation; -import org.overture.ast.lex.Dialect; -import org.overture.ast.lex.LexIdentifierToken; -import org.overture.ast.lex.LexNameToken; -import org.overture.ast.lex.LexToken; -import org.overture.ast.lex.VDMToken; +import org.overture.ast.lex.*; import org.overture.ast.patterns.ADefPatternBind; import org.overture.ast.patterns.PMultipleBind; import org.overture.ast.patterns.PPattern; -import org.overture.ast.statements.AApplyObjectDesignator; -import org.overture.ast.statements.AAssignmentStm; -import org.overture.ast.statements.ABlockSimpleBlockStm; -import org.overture.ast.statements.ACaseAlternativeStm; -import org.overture.ast.statements.ACasesStm; -import org.overture.ast.statements.AElseIfStm; -import org.overture.ast.statements.AFieldObjectDesignator; -import org.overture.ast.statements.AIdentifierObjectDesignator; -import org.overture.ast.statements.ALetBeStStm; -import org.overture.ast.statements.ALetStm; -import org.overture.ast.statements.ANonDeterministicSimpleBlockStm; -import org.overture.ast.statements.ASpecificationStm; -import org.overture.ast.statements.ATixeStmtAlternative; -import org.overture.ast.statements.PObjectDesignator; -import org.overture.ast.statements.PStateDesignator; -import org.overture.ast.statements.PStm; +import org.overture.ast.statements.*; import org.overture.ast.typechecker.NameScope; import org.overture.ast.types.PType; import org.overture.config.Release; @@ -62,6 +40,9 @@ import org.overture.parser.lex.LexException; import org.overture.parser.lex.LexTokenReader; +import java.util.List; +import java.util.Vector; + /** * A syntax analyser to parse statements. */ @@ -943,7 +924,7 @@ private List readCaseAlternatives() return alts; } - private ALetStm readDefStatement(ILexLocation token) + private ADefStm readDefStatement(ILexLocation token) throws ParserException, LexException { checkFor(VDMToken.DEF, 2239, "Expecting 'def'"); @@ -958,7 +939,7 @@ private ALetStm readDefStatement(ILexLocation token) checkFor(VDMToken.IN, 2240, "Expecting 'in' after equals definitions"); - return AstFactory.newALetStm(token, equalsDefs, readStatement()); + return AstFactory.newADefStm(token, equalsDefs, readStatement()); } private ASpecificationStm readSpecStatement(ILexLocation token) diff --git a/core/pog/src/main/java/org/overture/pog/visitors/PogParamStmVisitor.java b/core/pog/src/main/java/org/overture/pog/visitors/PogParamStmVisitor.java index ea171e8e5e..6d5d65ff2b 100644 --- a/core/pog/src/main/java/org/overture/pog/visitors/PogParamStmVisitor.java +++ b/core/pog/src/main/java/org/overture/pog/visitors/PogParamStmVisitor.java @@ -10,45 +10,9 @@ import org.overture.ast.patterns.ASeqBind; import org.overture.ast.patterns.ASetBind; import org.overture.ast.patterns.ATypeBind; -import org.overture.ast.statements.AAlwaysStm; -import org.overture.ast.statements.AAssignmentStm; -import org.overture.ast.statements.AAtomicStm; -import org.overture.ast.statements.ABlockSimpleBlockStm; -import org.overture.ast.statements.ACallObjectStm; -import org.overture.ast.statements.ACallStm; -import org.overture.ast.statements.ACaseAlternativeStm; -import org.overture.ast.statements.ACasesStm; -import org.overture.ast.statements.AElseIfStm; -import org.overture.ast.statements.AErrorCase; -import org.overture.ast.statements.AExitStm; -import org.overture.ast.statements.AForAllStm; -import org.overture.ast.statements.AForIndexStm; -import org.overture.ast.statements.AForPatternBindStm; -import org.overture.ast.statements.AIfStm; -import org.overture.ast.statements.ALetBeStStm; -import org.overture.ast.statements.ALetStm; -import org.overture.ast.statements.AReturnStm; -import org.overture.ast.statements.ASpecificationStm; -import org.overture.ast.statements.AStartStm; -import org.overture.ast.statements.ATixeStm; -import org.overture.ast.statements.ATixeStmtAlternative; -import org.overture.ast.statements.ATrapStm; -import org.overture.ast.statements.AWhileStm; -import org.overture.ast.statements.PStm; -import org.overture.ast.statements.SSimpleBlockStm; -import org.overture.pog.contexts.AssignmentContext; -import org.overture.pog.contexts.OpPostConditionContext; -import org.overture.pog.contexts.POForAllContext; -import org.overture.pog.contexts.PONameContext; -import org.overture.pog.contexts.POScopeContext; -import org.overture.pog.obligation.LetBeExistsObligation; -import org.overture.pog.obligation.OperationCallObligation; -import org.overture.pog.obligation.ProofObligationList; -import org.overture.pog.obligation.SeqMembershipObligation; -import org.overture.pog.obligation.SetMembershipObligation; -import org.overture.pog.obligation.StateInvariantObligation; -import org.overture.pog.obligation.TypeCompatibilityObligation; -import org.overture.pog.obligation.WhileLoopObligation; +import org.overture.ast.statements.*; +import org.overture.pog.contexts.*; +import org.overture.pog.obligation.*; import org.overture.pog.pub.IPOContextStack; import org.overture.pog.pub.IPogAssistantFactory; import org.overture.pog.pub.IProofObligationList; @@ -669,6 +633,33 @@ public IProofObligationList caseALetStm(ALetStm node, } } + @Override + public IProofObligationList caseADefStm(ADefStm node, + IPOContextStack question) throws AnalysisException + { + try + { + IProofObligationList obligations = new ProofObligationList(); + + for (PDefinition localDef : node.getLocalDefs()) + { + // PDefinitionAssistantTC.get + question.push(new PONameContext(aF.createPDefinitionAssistant().getVariableNames(localDef))); + obligations.addAll(localDef.apply(rootVisitor, question)); + question.pop(); + } + + question.push(new POScopeContext()); + obligations.addAll(node.getStatement().apply(mainVisitor, question)); + question.pop(); + + return obligations; + } catch (Exception e) + { + throw new POException(node, e.getMessage()); + } + } + public IProofObligationList defaultSSimpleBlockStm(SSimpleBlockStm node, IPOContextStack question) throws AnalysisException { diff --git a/core/typechecker/src/main/java/org/overture/typechecker/utilities/FreeVariablesChecker.java b/core/typechecker/src/main/java/org/overture/typechecker/utilities/FreeVariablesChecker.java index aa945e0b9b..0e94f9a2cd 100644 --- a/core/typechecker/src/main/java/org/overture/typechecker/utilities/FreeVariablesChecker.java +++ b/core/typechecker/src/main/java/org/overture/typechecker/utilities/FreeVariablesChecker.java @@ -23,113 +23,23 @@ package org.overture.typechecker.utilities; -import java.util.List; -import java.util.Vector; - import org.overture.ast.analysis.AnalysisException; import org.overture.ast.analysis.QuestionAnswerAdaptor; -import org.overture.ast.definitions.AAssignmentDefinition; -import org.overture.ast.definitions.AClassInvariantDefinition; -import org.overture.ast.definitions.AEqualsDefinition; -import org.overture.ast.definitions.AExplicitFunctionDefinition; -import org.overture.ast.definitions.AExplicitOperationDefinition; -import org.overture.ast.definitions.AImplicitFunctionDefinition; -import org.overture.ast.definitions.AImplicitOperationDefinition; -import org.overture.ast.definitions.AInstanceVariableDefinition; -import org.overture.ast.definitions.ALocalDefinition; -import org.overture.ast.definitions.ARenamedDefinition; -import org.overture.ast.definitions.AStateDefinition; -import org.overture.ast.definitions.ATypeDefinition; -import org.overture.ast.definitions.AValueDefinition; -import org.overture.ast.definitions.PDefinition; -import org.overture.ast.expressions.AApplyExp; -import org.overture.ast.expressions.ACasesExp; -import org.overture.ast.expressions.ADefExp; -import org.overture.ast.expressions.AExists1Exp; -import org.overture.ast.expressions.AExistsExp; -import org.overture.ast.expressions.AFieldExp; -import org.overture.ast.expressions.AForAllExp; -import org.overture.ast.expressions.AFuncInstatiationExp; -import org.overture.ast.expressions.AIfExp; -import org.overture.ast.expressions.AIotaExp; -import org.overture.ast.expressions.AIsExp; -import org.overture.ast.expressions.ALambdaExp; -import org.overture.ast.expressions.ALetBeStExp; -import org.overture.ast.expressions.ALetDefExp; -import org.overture.ast.expressions.AMapCompMapExp; -import org.overture.ast.expressions.AMapEnumMapExp; -import org.overture.ast.expressions.AMapletExp; -import org.overture.ast.expressions.AMkBasicExp; -import org.overture.ast.expressions.AMkTypeExp; -import org.overture.ast.expressions.AMuExp; -import org.overture.ast.expressions.ANarrowExp; -import org.overture.ast.expressions.ARecordModifier; -import org.overture.ast.expressions.ASeqCompSeqExp; -import org.overture.ast.expressions.ASeqEnumSeqExp; -import org.overture.ast.expressions.ASetCompSetExp; -import org.overture.ast.expressions.ASetEnumSetExp; -import org.overture.ast.expressions.ASetRangeSetExp; -import org.overture.ast.expressions.ASubseqExp; -import org.overture.ast.expressions.ATupleExp; -import org.overture.ast.expressions.AVariableExp; -import org.overture.ast.expressions.PExp; -import org.overture.ast.expressions.SBinaryExp; -import org.overture.ast.expressions.SBooleanBinaryExp; -import org.overture.ast.expressions.SUnaryExp; +import org.overture.ast.definitions.*; +import org.overture.ast.expressions.*; import org.overture.ast.factory.AstFactory; import org.overture.ast.lex.LexNameSet; import org.overture.ast.node.INode; -import org.overture.ast.patterns.APatternListTypePair; -import org.overture.ast.patterns.ASeqBind; -import org.overture.ast.patterns.ASeqMultipleBind; -import org.overture.ast.patterns.ASetBind; -import org.overture.ast.patterns.ASetMultipleBind; -import org.overture.ast.patterns.ATypeBind; -import org.overture.ast.patterns.ATypeMultipleBind; -import org.overture.ast.patterns.PMultipleBind; -import org.overture.ast.patterns.PPattern; -import org.overture.ast.statements.AAlwaysStm; -import org.overture.ast.statements.AAssignmentStm; -import org.overture.ast.statements.AAtomicStm; -import org.overture.ast.statements.ABlockSimpleBlockStm; -import org.overture.ast.statements.ACallObjectStm; -import org.overture.ast.statements.ACallStm; -import org.overture.ast.statements.ACasesStm; -import org.overture.ast.statements.ACyclesStm; -import org.overture.ast.statements.ADurationStm; -import org.overture.ast.statements.AExitStm; -import org.overture.ast.statements.AForAllStm; -import org.overture.ast.statements.AForIndexStm; -import org.overture.ast.statements.AForPatternBindStm; -import org.overture.ast.statements.AIfStm; -import org.overture.ast.statements.ALetBeStStm; -import org.overture.ast.statements.ALetStm; -import org.overture.ast.statements.APeriodicStm; -import org.overture.ast.statements.AReturnStm; -import org.overture.ast.statements.ASporadicStm; -import org.overture.ast.statements.AStartStm; -import org.overture.ast.statements.AStopStm; -import org.overture.ast.statements.ATixeStm; -import org.overture.ast.statements.ATrapStm; -import org.overture.ast.statements.AWhileStm; -import org.overture.ast.statements.PStm; -import org.overture.ast.statements.SSimpleBlockStm; +import org.overture.ast.patterns.*; +import org.overture.ast.statements.*; import org.overture.ast.typechecker.NameScope; -import org.overture.ast.types.ABracketType; -import org.overture.ast.types.AFunctionType; -import org.overture.ast.types.AMapMapType; -import org.overture.ast.types.ANamedInvariantType; -import org.overture.ast.types.AOptionalType; -import org.overture.ast.types.AProductType; -import org.overture.ast.types.ARecordInvariantType; -import org.overture.ast.types.ASeq1SeqType; -import org.overture.ast.types.ASeqSeqType; -import org.overture.ast.types.ASet1SetType; -import org.overture.ast.types.ASetSetType; -import org.overture.ast.types.PType; +import org.overture.ast.types.*; import org.overture.typechecker.FlatEnvironment; import org.overture.typechecker.assistant.ITypeCheckerAssistantFactory; +import java.util.List; +import java.util.Vector; + /** * Used to find the free variables in a definition, type or expression. */ @@ -1102,7 +1012,30 @@ public LexNameSet caseALetStm(ALetStm node, FreeVarInfo info) throws AnalysisExc names.addAll(node.getStatement().apply(this, local)); return names; } - + + @Override + public LexNameSet caseADefStm(ADefStm node, FreeVarInfo info) throws AnalysisException + { + FreeVarInfo local = info; + LexNameSet names = new LexNameSet(); + + for (PDefinition d : node.getLocalDefs()) + { + if (d instanceof AExplicitFunctionDefinition) + { + // ignore + } + else + { + local = info.set(new FlatEnvironment(af, d, local.env)); + names.addAll(d.apply(this, local)); + } + } + + names.addAll(node.getStatement().apply(this, local)); + return names; + } + @Override public LexNameSet caseAPeriodicStm(APeriodicStm node, FreeVarInfo info) throws AnalysisException { diff --git a/core/typechecker/src/main/java/org/overture/typechecker/visitor/TypeCheckerStmVisitor.java b/core/typechecker/src/main/java/org/overture/typechecker/visitor/TypeCheckerStmVisitor.java index 3b9b452379..4c238734ff 100644 --- a/core/typechecker/src/main/java/org/overture/typechecker/visitor/TypeCheckerStmVisitor.java +++ b/core/typechecker/src/main/java/org/overture/typechecker/visitor/TypeCheckerStmVisitor.java @@ -21,20 +21,9 @@ */ package org.overture.typechecker.visitor; -import java.util.LinkedList; -import java.util.List; -import java.util.Set; -import java.util.Vector; -import java.util.Map.Entry; - import org.overture.ast.analysis.AnalysisException; import org.overture.ast.analysis.intf.IQuestionAnswer; -import org.overture.ast.definitions.AExplicitOperationDefinition; -import org.overture.ast.definitions.AImplicitOperationDefinition; -import org.overture.ast.definitions.AInstanceVariableDefinition; -import org.overture.ast.definitions.AMultiBindListDefinition; -import org.overture.ast.definitions.PDefinition; -import org.overture.ast.definitions.SClassDefinition; +import org.overture.ast.definitions.*; import org.overture.ast.expressions.ABooleanConstExp; import org.overture.ast.expressions.ASelfExp; import org.overture.ast.expressions.AVariableExp; @@ -44,74 +33,22 @@ import org.overture.ast.lex.Dialect; import org.overture.ast.lex.LexNameToken; import org.overture.ast.lex.LexStringToken; -import org.overture.ast.patterns.ADefPatternBind; -import org.overture.ast.patterns.AExpressionPattern; -import org.overture.ast.patterns.ASetBind; -import org.overture.ast.patterns.ATypeBind; -import org.overture.ast.patterns.PBind; -import org.overture.ast.statements.AAlwaysStm; -import org.overture.ast.statements.AAssignmentStm; -import org.overture.ast.statements.AAtomicStm; -import org.overture.ast.statements.ABlockSimpleBlockStm; -import org.overture.ast.statements.ACallObjectStm; -import org.overture.ast.statements.ACallStm; -import org.overture.ast.statements.ACaseAlternativeStm; -import org.overture.ast.statements.ACasesStm; -import org.overture.ast.statements.AClassInvariantStm; -import org.overture.ast.statements.ACyclesStm; -import org.overture.ast.statements.ADurationStm; -import org.overture.ast.statements.AElseIfStm; -import org.overture.ast.statements.AErrorCase; -import org.overture.ast.statements.AErrorStm; -import org.overture.ast.statements.AExitStm; -import org.overture.ast.statements.AExternalClause; -import org.overture.ast.statements.AForAllStm; -import org.overture.ast.statements.AForIndexStm; -import org.overture.ast.statements.AForPatternBindStm; -import org.overture.ast.statements.AIdentifierStateDesignator; -import org.overture.ast.statements.AIfStm; -import org.overture.ast.statements.ALetBeStStm; -import org.overture.ast.statements.ALetStm; -import org.overture.ast.statements.ANonDeterministicSimpleBlockStm; -import org.overture.ast.statements.ANotYetSpecifiedStm; -import org.overture.ast.statements.APeriodicStm; -import org.overture.ast.statements.AReturnStm; -import org.overture.ast.statements.ASkipStm; -import org.overture.ast.statements.ASpecificationStm; -import org.overture.ast.statements.ASporadicStm; -import org.overture.ast.statements.AStartStm; -import org.overture.ast.statements.AStopStm; -import org.overture.ast.statements.ASubclassResponsibilityStm; -import org.overture.ast.statements.ATixeStm; -import org.overture.ast.statements.ATixeStmtAlternative; -import org.overture.ast.statements.ATrapStm; -import org.overture.ast.statements.AWhileStm; -import org.overture.ast.statements.PStateDesignator; -import org.overture.ast.statements.PStm; -import org.overture.ast.statements.SSimpleBlockStm; +import org.overture.ast.patterns.*; +import org.overture.ast.statements.*; import org.overture.ast.typechecker.NameScope; -import org.overture.ast.types.ABooleanBasicType; -import org.overture.ast.types.AClassType; -import org.overture.ast.types.AFunctionType; -import org.overture.ast.types.AOperationType; -import org.overture.ast.types.SSetType; -import org.overture.ast.types.AUnionType; -import org.overture.ast.types.AUnknownType; -import org.overture.ast.types.AVoidReturnType; -import org.overture.ast.types.AVoidType; -import org.overture.ast.types.PType; +import org.overture.ast.types.*; import org.overture.ast.util.PTypeSet; import org.overture.config.Settings; -import org.overture.typechecker.Environment; -import org.overture.typechecker.FlatCheckedEnvironment; -import org.overture.typechecker.FlatEnvironment; -import org.overture.typechecker.PrivateClassEnvironment; -import org.overture.typechecker.PublicClassEnvironment; -import org.overture.typechecker.TypeCheckInfo; -import org.overture.typechecker.TypeCheckerErrors; +import org.overture.typechecker.*; import org.overture.typechecker.assistant.type.AClassTypeAssistantTC; import org.overture.typechecker.utilities.type.QualifiedDefinition; +import java.util.LinkedList; +import java.util.List; +import java.util.Map.Entry; +import java.util.Set; +import java.util.Vector; + public class TypeCheckerStmVisitor extends AbstractTypeCheckVisitor { @@ -658,8 +595,6 @@ public PType caseACyclesStm(ACyclesStm node, TypeCheckInfo question) return node.getStatement().apply(THIS, question); } - // TODO: Missing the other DefStatement - @Override public PType caseALetStm(ALetStm node, TypeCheckInfo question) throws AnalysisException @@ -668,6 +603,14 @@ public PType caseALetStm(ALetStm node, TypeCheckInfo question) return node.getType(); } + @Override + public PType caseADefStm(ADefStm node, TypeCheckInfo question) + throws AnalysisException + { + node.setType(typeCheckLet(node, node.getLocalDefs(),node.getStatement(),question)); + return node.getType(); + } + @Override public PType caseADurationStm(ADurationStm node, TypeCheckInfo question) throws AnalysisException