diff --git a/Src/PChecker/CheckerCore/Actors/Events/Event.cs b/Src/PChecker/CheckerCore/Actors/Events/Event.cs index ca9fe6b4d5..57917c052b 100644 --- a/Src/PChecker/CheckerCore/Actors/Events/Event.cs +++ b/Src/PChecker/CheckerCore/Actors/Events/Event.cs @@ -11,7 +11,6 @@ namespace PChecker.Actors.Events [DataContract] public abstract class Event { - public int Loc { get; set; } public string? Sender; public string? Receiver; public string? State; diff --git a/Src/PChecker/CheckerCore/PRuntime/PEvent.cs b/Src/PChecker/CheckerCore/PRuntime/PEvent.cs index 8059cd490d..33af7d6790 100644 --- a/Src/PChecker/CheckerCore/PRuntime/PEvent.cs +++ b/Src/PChecker/CheckerCore/PRuntime/PEvent.cs @@ -6,15 +6,13 @@ namespace PChecker.PRuntime { public class PEvent : Event, IPrtValue { - public PEvent(int loc) : base() + public PEvent() : base() { - Loc = loc; } - public PEvent(IPrtValue payload, int loc) : base() + public PEvent(IPrtValue payload) : base() { Payload = payload; - Loc = loc; } public IPrtValue Payload { get; } @@ -52,7 +50,7 @@ public override string ToString() public class PHalt : PEvent { - public PHalt(IPrtValue payload, int loc) : base(payload, loc) + public PHalt(IPrtValue payload) : base(payload) { } } diff --git a/Src/PChecker/CheckerCore/PRuntime/PMachine.cs b/Src/PChecker/CheckerCore/PRuntime/PMachine.cs index d23ef2c457..d95b2e38de 100644 --- a/Src/PChecker/CheckerCore/PRuntime/PMachine.cs +++ b/Src/PChecker/CheckerCore/PRuntime/PMachine.cs @@ -84,7 +84,7 @@ public void TrySendEvent(PMachineValue target, Event ev, object payload = null) Assert(target.Permissions.Contains(ev.GetType().Name), $"Event {ev.GetType().Name} is not in the permissions set of the target machine"); var oneArgConstructor = ev.GetType().GetConstructors().First(x => x.GetParameters().Length > 0); - ev = (Event)oneArgConstructor.Invoke(new[] { payload , ev.Loc}); + ev = (Event)oneArgConstructor.Invoke(new[] { payload }); ev.Sender = Id.ToString(); ev.Receiver = target.Id.ToString(); @@ -96,7 +96,7 @@ public void TryRaiseEvent(Event ev, object payload = null) { Assert(ev != null, "Machine cannot raise a null event"); var oneArgConstructor = ev.GetType().GetConstructors().First(x => x.GetParameters().Length > 0); - ev = (Event)oneArgConstructor.Invoke(new[] { payload, ev.Loc }); + ev = (Event)oneArgConstructor.Invoke(new[] { payload }); RaiseEvent(ev); throw new PNonStandardReturnException { ReturnKind = NonStandardReturn.Raise }; } @@ -190,7 +190,7 @@ public void Announce(Event ev, object payload = null) } var oneArgConstructor = ev.GetType().GetConstructors().First(x => x.GetParameters().Length > 0); - var @event = (Event)oneArgConstructor.Invoke(new[] { payload, ev.Loc }); + var @event = (Event)oneArgConstructor.Invoke(new[] { payload }); var pText = payload == null ? "" : $" with payload {((IPrtValue)payload).ToEscapedString()}"; Logger.WriteLine($" '{Id}' announced event '{ev.GetType().Name}'{pText}."); @@ -255,7 +255,7 @@ public object ToDict() public class InitializeParametersEvent : PEvent { - public InitializeParametersEvent(InitializeParameters payload) : base(payload, 0) + public InitializeParametersEvent(InitializeParameters payload) : base(payload) { } } diff --git a/Src/PCompiler/CompilerCore/Backend/CSharp/CSharpCodeGenerator.cs b/Src/PCompiler/CompilerCore/Backend/CSharp/CSharpCodeGenerator.cs index 07a7d17d74..c7a2223bc3 100644 --- a/Src/PCompiler/CompilerCore/Backend/CSharp/CSharpCodeGenerator.cs +++ b/Src/PCompiler/CompilerCore/Backend/CSharp/CSharpCodeGenerator.cs @@ -22,8 +22,6 @@ public class CSharpCodeGenerator : ICodeGenerator /// public bool HasCompilationStage => true; - private int _sendEventIndex = 0; - public void Compile(ICompilerConfiguration job) { var csprojName = $"{job.ProjectName}.csproj"; @@ -486,8 +484,8 @@ private void WriteEvent(CompilationContext context, StringWriter output, PEvent var payloadType = GetCSharpType(pEvent.PayloadType, true); context.WriteLine(output, $"internal partial class {declName} : PEvent"); context.WriteLine(output, "{"); - context.WriteLine(output, $"public {declName}() : base(-1) {{}}"); - context.WriteLine(output, $"public {declName}({payloadType} payload, int loc): base(payload, loc)" + "{ }"); + context.WriteLine(output, $"public {declName}() : base() {{}}"); + context.WriteLine(output, $"public {declName} ({payloadType} payload): base(payload)" + "{ }"); context.WriteLine(output, $"public override IPrtValue Clone() {{ return new {declName}();}}"); context.WriteLine(output, "}"); @@ -512,12 +510,12 @@ private void WriteMachine(CompilationContext context, StringWriter output, Machi var cTorType = GetCSharpType(machine.PayloadType, true); context.Write(output, "public class ConstructorEvent : PEvent"); context.Write(output, "{"); - context.Write(output, $"public ConstructorEvent({cTorType} val, int loc) : base(val, loc) {{ }}"); + context.Write(output, $"public ConstructorEvent({cTorType} val) : base(val) {{ }}"); context.WriteLine(output, "}"); context.WriteLine(output); context.WriteLine(output, - $"protected override Event GetConstructorEvent(IPrtValue value) {{ return new ConstructorEvent(({cTorType})value, {_sendEventIndex++}); }}"); + $"protected override Event GetConstructorEvent(IPrtValue value) {{ return new ConstructorEvent(({cTorType})value); }}"); // create the constructor to initialize the sends, creates and receives list WriteMachineConstructor(context, output, machine); @@ -708,7 +706,7 @@ private void WriteFunction(CompilationContext context, StringWriter output, Func var staticKeyword = isStatic ? "static " : ""; var asyncKeyword = isAsync ? "async " : ""; - var returnType = function.Role != FunctionRole.Scenario ? GetCSharpType(signature.ReturnType) : "int"; + var returnType = GetCSharpType(signature.ReturnType); if (isAsync) { @@ -721,10 +719,6 @@ private void WriteFunction(CompilationContext context, StringWriter output, Func { functionParameters = "Event currentMachine_dequeuedEvent"; } - else if (function.Role == FunctionRole.Scenario) - { - functionParameters = "List events"; - } else { functionParameters = string.Join( @@ -733,7 +727,7 @@ private void WriteFunction(CompilationContext context, StringWriter output, Func $"{GetCSharpType(param.Type)} {context.Names.GetNameForDecl(param)}")); } - if (isStatic && function.Role != FunctionRole.Scenario) // then we need to generate two versions of the function + if (isStatic) // then we need to generate two versions of the function { // for machine var seperator = functionParameters == "" ? "" : ", "; @@ -789,69 +783,14 @@ private void WriteFunctionBody(CompilationContext context, StringWriter output, $"{GetCSharpType(type, true)} {context.Names.GetNameForDecl(local)} = {GetDefaultValue(type)};"); } - if (function.Role != FunctionRole.Scenario) - { - foreach (var bodyStatement in function.Body.Statements) - { - WriteStmt(context: context, output: output, function: function, stmt: bodyStatement); - } - } - else - { - WriteScenario(context, output, function); - } - - - context.WriteLine(output, "}"); - } - - private void WriteScenario(CompilationContext context, StringWriter output, Function function) - { - int numOfStmt = function.Body.Statements.Count + 1; - context.WriteLine(output, $"int state = {numOfStmt};"); - var eventPredicates = string.Join(" or ", function.Signature.ParameterEvents.Select(it => it.Name)); - context.WriteLine(output, $"events = events.Where(it => it is {eventPredicates}).ToList();"); - WriteConstraintsRecursive(context, output, function, 0, new HashSet(), 0); - context.WriteLine(output, "return state;"); - } - - private void WriteConstraintsRecursive(CompilationContext context, StringWriter output, Function function, int index, HashSet visitedVariables, int satisfiedConstraints) - { - if (index >= function.Signature.Parameters.Count) - { - context.WriteLine(output, "return 1;"); - return; - } - var param = function.Signature.Parameters[index]; - var e = function.Signature.ParameterEvents[index]; - visitedVariables.Add(param); - var start = index == 0 ? "0" : $"i{index - 1} + 1"; - var paramName = context.Names.GetNameForDecl(param); - context.WriteLine(output, $"for (var i{index} = {start} ; i{index} < events.Count; i{index} ++) " + "{"); - context.WriteLine(output, $"var {paramName}_obj = events[i{index}];"); - context.WriteLine(output, $"if ({paramName}_obj is not {e.Name}) continue;"); - context.WriteLine(output, $"var {paramName} = ((PEvent) {paramName}_obj).Payload;"); - foreach (var bodyStatement in function.Body.Statements) { - if (bodyStatement is ConstraintStmt stmt) - { - var variables = ConstraintVariableCollector.FindVariablesRecursive(stmt.Constraint); - if (variables.Contains(param) && visitedVariables.IsSupersetOf(variables)) - { - context.Write(output, $"if (!("); - WriteExpr(context, output, stmt.Constraint); - context.WriteLine(output, $")) continue;"); - satisfiedConstraints += 1; - context.WriteLine(output, $"state = Math.Min({function.Body.Statements.Count - satisfiedConstraints + 1}, state);"); - } - } + WriteStmt(context: context, output: output, function: function, stmt: bodyStatement); } - WriteConstraintsRecursive(context, output, function, index + 1, visitedVariables, satisfiedConstraints); + context.WriteLine(output, "}"); } - private void WriteStmt(CompilationContext context, StringWriter output, Function function, IPStmt stmt) { switch (stmt) @@ -1240,19 +1179,9 @@ private void WriteLValue(CompilationContext context, StringWriter output, IPExpr break; case NamedTupleAccessExpr namedTupleAccessExpr: - if (ExprVisitor.ReservedEventFeilds.ContainsValue(namedTupleAccessExpr.Entry)) - { - var type = GetCSharpType(namedTupleAccessExpr.Entry.Type); - context.Write(output, $"(({type}) ("); - WriteExpr(context, output, namedTupleAccessExpr.SubExpr); - context.Write(output, $"_obj).{namedTupleAccessExpr.FieldName})"); - } - else - { - context.Write(output, "((PrtNamedTuple)"); - WriteExpr(context, output, namedTupleAccessExpr.SubExpr); - context.Write(output, $")[\"{namedTupleAccessExpr.FieldName}\"]"); - } + context.Write(output, "((PrtNamedTuple)"); + WriteExpr(context, output, namedTupleAccessExpr.SubExpr); + context.Write(output, $")[\"{namedTupleAccessExpr.FieldName}\"]"); break; case SeqAccessExpr seqAccessExpr: @@ -1296,9 +1225,9 @@ private void WriteExpr(CompilationContext context, StringWriter output, IPExpr p context.Write(output, $"({negate}PrtValues.SafeEquals("); if (PLanguageType.TypeIsOfKind(binOpExpr.Lhs.Type, TypeKind.Enum)) { - context.Write(output, "PrtValues.Box((long) ((PrtInt)"); + context.Write(output, "PrtValues.Box((long) "); WriteExpr(context, output, binOpExpr.Lhs); - context.Write(output, ")),"); + context.Write(output, "),"); } else { @@ -1308,9 +1237,9 @@ private void WriteExpr(CompilationContext context, StringWriter output, IPExpr p if (PLanguageType.TypeIsOfKind(binOpExpr.Rhs.Type, TypeKind.Enum)) { - context.Write(output, "PrtValues.Box((long) ((PrtInt)"); + context.Write(output, "PrtValues.Box((long) "); WriteExpr(context, output, binOpExpr.Rhs); - context.Write(output, "))"); + context.Write(output, ")"); } else { @@ -1459,7 +1388,7 @@ private void WriteExpr(CompilationContext context, StringWriter output, IPExpr p switch (eventName) { case "Halt": - context.Write(output, "new PHalt(" + _sendEventIndex++ + ")"); + context.Write(output, "new PHalt()"); break; case "DefaultEvent": @@ -1468,7 +1397,7 @@ private void WriteExpr(CompilationContext context, StringWriter output, IPExpr p default: var payloadExpr = GetDefaultValue(eventRefExpr.Value.PayloadType); - context.Write(output, $"new {eventName}({payloadExpr}, {_sendEventIndex++})"); + context.Write(output, $"new {eventName}({payloadExpr})"); break; } diff --git a/Src/PCompiler/CompilerCore/Backend/IRTransformer.cs b/Src/PCompiler/CompilerCore/Backend/IRTransformer.cs index 8e6992cf9b..943eba0df8 100644 --- a/Src/PCompiler/CompilerCore/Backend/IRTransformer.cs +++ b/Src/PCompiler/CompilerCore/Backend/IRTransformer.cs @@ -567,9 +567,6 @@ private List SimplifyStatement(IPStmt statement) condCheck.Concat(SimplifyStatement(whileStmt.Body))); return new List { new WhileStmt(location, new BoolLiteralExpr(location, true), loopBody) }; - // We do not rewrite constraint statements for now. - case ConstraintStmt constraintStmt: - return new List() { constraintStmt }; default: throw new ArgumentOutOfRangeException(nameof(statement)); diff --git a/Src/PCompiler/CompilerCore/Parser/PLexer.g4 b/Src/PCompiler/CompilerCore/Parser/PLexer.g4 index e8fb715019..8f7511ac8d 100644 --- a/Src/PCompiler/CompilerCore/Parser/PLexer.g4 +++ b/Src/PCompiler/CompilerCore/Parser/PLexer.g4 @@ -70,7 +70,6 @@ MODULE : 'module' ; IMPLEMENTATION : 'implementation' ; TEST : 'test' ; REFINES : 'refines' ; -SCENARIO : 'scenario' ; // module constructors COMPOSE : 'compose' ; diff --git a/Src/PCompiler/CompilerCore/Parser/PParser.g4 b/Src/PCompiler/CompilerCore/Parser/PParser.g4 index 3140b37edb..dc00fca475 100644 --- a/Src/PCompiler/CompilerCore/Parser/PParser.g4 +++ b/Src/PCompiler/CompilerCore/Parser/PParser.g4 @@ -60,7 +60,6 @@ topDecl : typeDefDecl | namedModuleDecl | testDecl | implementationDecl - | scenarioDecl ; @@ -108,13 +107,6 @@ funDecl : FUN name=iden LPAREN funParamList? RPAREN (COLON type)? (CREATES inter | FUN name=iden LPAREN funParamList? RPAREN (COLON type)? functionBody # PFunDecl ; -scenarioDecl : SCENARIO scenarioName=iden LPAREN scenarioParamList RPAREN scenarioBody; - -scenarioParamList: scenarioParam (COMMA scenarioParam)*; -scenarioParam : name=iden COLON nonDefaultEvent; -scenarioBody : LBRACE expr (COMMA expr)* RBRACE; - - stateDecl : START? temperature=(HOT | COLD)? STATE name=iden LBRACE stateBodyItem* RBRACE ; stateBodyItem : ENTRY anonEventHandler # StateEntry diff --git a/Src/PCompiler/CompilerCore/TypeChecker/AST/Declarations/Function.cs b/Src/PCompiler/CompilerCore/TypeChecker/AST/Declarations/Function.cs index c5001cbca9..69ed97c4ad 100644 --- a/Src/PCompiler/CompilerCore/TypeChecker/AST/Declarations/Function.cs +++ b/Src/PCompiler/CompilerCore/TypeChecker/AST/Declarations/Function.cs @@ -15,8 +15,7 @@ public enum FunctionRole EventHandler = 1 << 4, ExitHandler = 1 << 5, ReceiveHandler = 1 << 6, - Foreign = 1 << 7, - Scenario = 1 << 8, + Foreign = 1 << 7 } public class Function : IPDecl, IHasScope @@ -33,8 +32,7 @@ sourceNode is PParser.AnonEventHandlerContext || sourceNode is PParser.NoParamAnonEventHandlerContext || sourceNode is PParser.ReceiveStmtContext || sourceNode is PParser.WhileStmtContext || - sourceNode is PParser.ForeachStmtContext || - sourceNode is PParser.ScenarioDeclContext); + sourceNode is PParser.ForeachStmtContext); Name = name; SourceLocation = sourceNode; CanCreate = false; diff --git a/Src/PCompiler/CompilerCore/TypeChecker/AST/Declarations/FunctionSignature.cs b/Src/PCompiler/CompilerCore/TypeChecker/AST/Declarations/FunctionSignature.cs index c3801faab7..b711938af6 100644 --- a/Src/PCompiler/CompilerCore/TypeChecker/AST/Declarations/FunctionSignature.cs +++ b/Src/PCompiler/CompilerCore/TypeChecker/AST/Declarations/FunctionSignature.cs @@ -7,8 +7,6 @@ namespace Plang.Compiler.TypeChecker.AST.Declarations public class FunctionSignature { public List Parameters { get; } = new List(); - // This is only used by scenarios. - public List ParameterEvents { get; } = new(); public IEnumerable ParameterTypes => Parameters.Select(ty => ty.Type); public PLanguageType ReturnType { get; set; } = PrimitiveType.Null; } diff --git a/Src/PCompiler/CompilerCore/TypeChecker/AST/Statements/ConstraintStmt.cs b/Src/PCompiler/CompilerCore/TypeChecker/AST/Statements/ConstraintStmt.cs deleted file mode 100644 index 163e1a5953..0000000000 --- a/Src/PCompiler/CompilerCore/TypeChecker/AST/Statements/ConstraintStmt.cs +++ /dev/null @@ -1,15 +0,0 @@ -using Antlr4.Runtime; - -namespace Plang.Compiler.TypeChecker.AST.Statements; - -public class ConstraintStmt : IPStmt -{ - public ConstraintStmt(ParserRuleContext sourceLocation, IPExpr constraint) - { - SourceLocation = sourceLocation; - Constraint = constraint; - } - - public IPExpr Constraint { get; } - public ParserRuleContext SourceLocation { get; } -} \ No newline at end of file diff --git a/Src/PCompiler/CompilerCore/TypeChecker/Analyzer.cs b/Src/PCompiler/CompilerCore/TypeChecker/Analyzer.cs index b395a56024..3139ff3f93 100644 --- a/Src/PCompiler/CompilerCore/TypeChecker/Analyzer.cs +++ b/Src/PCompiler/CompilerCore/TypeChecker/Analyzer.cs @@ -179,14 +179,7 @@ private static Scope BuildGlobalScope(ICompilerConfiguration config, PParser.Pro { DeclarationVisitor.PopulateDeclarations(config.Handler, globalScope, programUnit, nodesToDeclarations); } - - // Step 3: Assign param types for scenario events. We have do this after all events are initialized. - foreach (var function in globalScope.Functions) - { - ScenarioEventVisitor.PopulateEventTypes(function); - } - - + return globalScope; } diff --git a/Src/PCompiler/CompilerCore/TypeChecker/ConstraintVariableCollector.cs b/Src/PCompiler/CompilerCore/TypeChecker/ConstraintVariableCollector.cs deleted file mode 100644 index 647d9c1168..0000000000 --- a/Src/PCompiler/CompilerCore/TypeChecker/ConstraintVariableCollector.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using Plang.Compiler.TypeChecker.AST; -using Plang.Compiler.TypeChecker.AST.Declarations; -using Plang.Compiler.TypeChecker.AST.Expressions; - -namespace Plang.Compiler.TypeChecker; - -public class ConstraintVariableCollector -{ - - public static HashSet FindVariablesRecursive(IPExpr expr) - { - switch (expr) - { - case BinOpExpr binOp: - return FindVariablesRecursive(binOp.Lhs).Union(FindVariablesRecursive(binOp.Rhs)).ToHashSet(); - case NamedTupleAccessExpr namedTupleAccessExpr: - return FindVariablesRecursive(namedTupleAccessExpr.SubExpr); - case VariableAccessExpr variableAccessExpr: - return new HashSet() { variableAccessExpr.Variable }; - case EnumElemRefExpr: - case StringExpr: - case IntLiteralExpr: - break; - default: - throw new ArgumentOutOfRangeException(nameof(expr)); - } - - return new HashSet(); - } -} \ No newline at end of file diff --git a/Src/PCompiler/CompilerCore/TypeChecker/ControlFlowChecker.cs b/Src/PCompiler/CompilerCore/TypeChecker/ControlFlowChecker.cs index bddcbbf892..ea09002934 100644 --- a/Src/PCompiler/CompilerCore/TypeChecker/ControlFlowChecker.cs +++ b/Src/PCompiler/CompilerCore/TypeChecker/ControlFlowChecker.cs @@ -84,7 +84,6 @@ private void CheckStmt(IPStmt stmt) case RemoveStmt _: case ReturnStmt _: case SendStmt _: - case ConstraintStmt _: break; default: diff --git a/Src/PCompiler/CompilerCore/TypeChecker/DeclarationStubVisitor.cs b/Src/PCompiler/CompilerCore/TypeChecker/DeclarationStubVisitor.cs index c2bf47b518..77782e7308 100644 --- a/Src/PCompiler/CompilerCore/TypeChecker/DeclarationStubVisitor.cs +++ b/Src/PCompiler/CompilerCore/TypeChecker/DeclarationStubVisitor.cs @@ -163,26 +163,6 @@ public override object VisitStateDecl(PParser.StateDeclContext context) #endregion Machines - #region Scenario - - public override object VisitScenarioDecl(PParser.ScenarioDeclContext context) - { - var scenarioName = context.scenarioName.GetText(); - var scenario = CurrentScope.Put(scenarioName, context); - nodesToDeclarations.Put(context, scenario); - return VisitChildrenWithNewScope(scenario, context); - } - - public override object VisitScenarioParam(PParser.ScenarioParamContext context) - { - var symbolName = context.name.GetText(); - var decl = CurrentScope.Put(symbolName, context, VariableRole.Param); - nodesToDeclarations.Put(context, decl); - return null; - } - - #endregion - #region Functions public override object VisitPFunDecl(PParser.PFunDeclContext context) diff --git a/Src/PCompiler/CompilerCore/TypeChecker/DeclarationVisitor.cs b/Src/PCompiler/CompilerCore/TypeChecker/DeclarationVisitor.cs index 0c02c0a2a9..82faffc717 100644 --- a/Src/PCompiler/CompilerCore/TypeChecker/DeclarationVisitor.cs +++ b/Src/PCompiler/CompilerCore/TypeChecker/DeclarationVisitor.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Diagnostics; using System.Linq; -using System.Runtime.CompilerServices; using Antlr4.Runtime.Tree; using Plang.Compiler.TypeChecker.AST; using Plang.Compiler.TypeChecker.AST.Declarations; @@ -349,30 +348,7 @@ public override object VisitSpecMachineDecl(PParser.SpecMachineDeclContext conte return specMachine; } - - public override object VisitScenarioDecl(PParser.ScenarioDeclContext context) - { - var scenario = (Function)nodesToDeclarations.Get(context); - var result = ((Variable, PEvent)[]) Visit(context.scenarioParamList()); - scenario.Signature.Parameters.AddRange(result.Select(it => it.Item1)); - scenario.Signature.ParameterEvents.AddRange(result.Select(it => it.Item2)); - scenario.Signature.ReturnType = PrimitiveType.Int; - scenario.Role = FunctionRole.Scenario; - return scenario; - } - - public override object VisitScenarioParamList(PParser.ScenarioParamListContext context) - { - return context.scenarioParam().Select(Visit).Cast<(Variable, PEvent)>().ToArray(); - } - - public override object VisitScenarioParam(PParser.ScenarioParamContext context) - { - var param = (Variable) nodesToDeclarations.Get(context); - var e = (PEvent) Visit(context.nonDefaultEvent()); - return (param, e); - } - + public override object VisitMachineBody(PParser.MachineBodyContext context) { foreach (var machineEntryContext in context.machineEntry()) diff --git a/Src/PCompiler/CompilerCore/TypeChecker/ExprVisitor.cs b/Src/PCompiler/CompilerCore/TypeChecker/ExprVisitor.cs index 3b5c325c78..7a85bf6e84 100644 --- a/Src/PCompiler/CompilerCore/TypeChecker/ExprVisitor.cs +++ b/Src/PCompiler/CompilerCore/TypeChecker/ExprVisitor.cs @@ -12,18 +12,6 @@ namespace Plang.Compiler.TypeChecker { public class ExprVisitor : PParserBaseVisitor { - public static readonly string EventFieldSender = "MSG_sender"; - public static readonly string EventFieldReceiver = "MSG_receiver"; - public static readonly string EventFieldState = "MSG_state"; - public static readonly string EventFieldIndex = "MSG_index"; - - public static Dictionary ReservedEventFeilds = new Dictionary() - { - { EventFieldSender, new NamedTupleEntry("Sender", -1, PrimitiveType.String) }, - { EventFieldReceiver, new NamedTupleEntry("Receiver", -1, PrimitiveType.String) }, - { EventFieldState, new NamedTupleEntry("State", -1, PrimitiveType.String) }, - { EventFieldIndex, new NamedTupleEntry("Index", -1, PrimitiveType.Int) }, - }; private readonly ITranslationErrorHandler handler; private readonly Function method; private readonly Scope table; @@ -58,18 +46,11 @@ public override IPExpr VisitParenExpr(PParser.ParenExprContext context) public override IPExpr VisitNamedTupleAccessExpr(PParser.NamedTupleAccessExprContext context) { var subExpr = Visit(context.expr()); - var fieldName = context.field.GetText(); - if (subExpr is VariableAccessExpr v && method.Role == FunctionRole.Scenario && method.Signature.Parameters.Contains(v.Variable)) - { - if (ReservedEventFeilds.TryGetValue(fieldName, out var reservedEntry)) - { - return new NamedTupleAccessExpr(context, subExpr, reservedEntry); - } - } if (!(subExpr.Type.Canonicalize() is NamedTupleType tuple)) { throw handler.TypeMismatch(subExpr, TypeKind.NamedTuple); } + var fieldName = context.field.GetText(); if (!tuple.LookupEntry(fieldName, out var entry)) { throw handler.MissingNamedTupleEntry(context.field, tuple); @@ -627,7 +608,7 @@ public override IPExpr VisitNamedTupleBody(PParser.NamedTupleBodyContext context } names.Add(entryName); - entries[i] = new NamedTupleEntry(name: entryName, fieldNo: i, type: fields[i].Type); + entries[i] = new NamedTupleEntry { Name = entryName, FieldNo = i, Type = fields[i].Type }; } var type = new NamedTupleType(entries); diff --git a/Src/PCompiler/CompilerCore/TypeChecker/FunctionBodyVisitor.cs b/Src/PCompiler/CompilerCore/TypeChecker/FunctionBodyVisitor.cs index b3572fb73f..2cb0b21eb3 100644 --- a/Src/PCompiler/CompilerCore/TypeChecker/FunctionBodyVisitor.cs +++ b/Src/PCompiler/CompilerCore/TypeChecker/FunctionBodyVisitor.cs @@ -1,9 +1,6 @@ -using System.Collections.Generic; using System.Diagnostics.Contracts; -using Plang.Compiler.TypeChecker.AST; using Plang.Compiler.TypeChecker.AST.Declarations; using Plang.Compiler.TypeChecker.AST.Statements; -using Plang.Compiler.TypeChecker.Types; namespace Plang.Compiler.TypeChecker { @@ -63,30 +60,6 @@ public override object VisitFunctionBody(PParser.FunctionBodyContext context) return null; } - public override object VisitScenarioDecl(PParser.ScenarioDeclContext context) - { - return Visit(context.scenarioBody()); - } - - public override object VisitScenarioBody(PParser.ScenarioBodyContext context) - { - var exprVisitor = new ExprVisitor(method, config.Handler); - // var compound = new CompoundStmt(context, new IPStmt[0]); - var stmts = new List(); - foreach (var exprContext in context.expr()) - { - var constraint = exprVisitor.Visit(exprContext); - if (!Equals(constraint.Type, PrimitiveType.Bool)) - { - throw config.Handler.TypeMismatch(exprContext, constraint.Type, PrimitiveType.Bool); - } - stmts.Add( new ConstraintStmt(exprContext, constraint)); - } - - method.Body = new CompoundStmt(context, stmts); - return null; - } - public override object VisitVarDecl(PParser.VarDeclContext context) { foreach (var varName in context.idenList()._names) diff --git a/Src/PCompiler/CompilerCore/TypeChecker/FunctionValidator.cs b/Src/PCompiler/CompilerCore/TypeChecker/FunctionValidator.cs index 35bd8a3be4..16b1204a1a 100644 --- a/Src/PCompiler/CompilerCore/TypeChecker/FunctionValidator.cs +++ b/Src/PCompiler/CompilerCore/TypeChecker/FunctionValidator.cs @@ -11,7 +11,7 @@ public static class FunctionValidator { public static void CheckAllPathsReturn(ITranslationErrorHandler handler, Function function) { - if (function.IsForeign || function.Role == FunctionRole.Scenario) + if (function.IsForeign) { return; } diff --git a/Src/PCompiler/CompilerCore/TypeChecker/ScenarioEventVisitor.cs b/Src/PCompiler/CompilerCore/TypeChecker/ScenarioEventVisitor.cs deleted file mode 100644 index 3cd4518397..0000000000 --- a/Src/PCompiler/CompilerCore/TypeChecker/ScenarioEventVisitor.cs +++ /dev/null @@ -1,20 +0,0 @@ -using Antlr4.Runtime.Tree; -using Plang.Compiler.TypeChecker.AST; -using Plang.Compiler.TypeChecker.AST.Declarations; - -namespace Plang.Compiler.TypeChecker; - -public class ScenarioEventVisitor -{ - - public static void PopulateEventTypes(Function function) - { - if (function.Role != FunctionRole.Scenario) return; - for (var i = 0; i < function.Signature.Parameters.Count; i++) - { - var v = function.Signature.Parameters[i]; - var e = function.Signature.ParameterEvents[i]; - v.Type = e.PayloadType; - } - } -} \ No newline at end of file diff --git a/Src/PCompiler/CompilerCore/TypeChecker/Scope.cs b/Src/PCompiler/CompilerCore/TypeChecker/Scope.cs index 22dc0b4bc8..9eff2b90ce 100644 --- a/Src/PCompiler/CompilerCore/TypeChecker/Scope.cs +++ b/Src/PCompiler/CompilerCore/TypeChecker/Scope.cs @@ -518,14 +518,6 @@ public Interface Put(string name, PParser.InterfaceDeclContext tree) return machineInterface; } - public Function Put(string name, PParser.ScenarioDeclContext tree) - { - var scenario = new Function(name, tree); - CheckConflicts(scenario, Namespace(functions)); - functions.Add(name, scenario); - return scenario; - } - public Machine Put(string name, PParser.ImplMachineDeclContext tree) { var machine = new Machine(name, tree); diff --git a/Src/PCompiler/CompilerCore/TypeChecker/TypeResolver.cs b/Src/PCompiler/CompilerCore/TypeChecker/TypeResolver.cs index 3fb843d8d9..39b22c3681 100644 --- a/Src/PCompiler/CompilerCore/TypeChecker/TypeResolver.cs +++ b/Src/PCompiler/CompilerCore/TypeChecker/TypeResolver.cs @@ -121,7 +121,7 @@ public override PLanguageType VisitNamedTupleType(PParser.NamedTupleTypeContext } names.Add(fieldName); - fields[i] = new NamedTupleEntry(name: fieldName, fieldNo: i, type: Visit(field.type())); + fields[i] = new NamedTupleEntry { Name = fieldName, FieldNo = i, Type = Visit(field.type()) }; } var ret = new NamedTupleType(fields); diff --git a/Src/PCompiler/CompilerCore/TypeChecker/Types/NamedTupleEntry.cs b/Src/PCompiler/CompilerCore/TypeChecker/Types/NamedTupleEntry.cs index 89ad48ae17..f9223f79c4 100644 --- a/Src/PCompiler/CompilerCore/TypeChecker/Types/NamedTupleEntry.cs +++ b/Src/PCompiler/CompilerCore/TypeChecker/Types/NamedTupleEntry.cs @@ -2,17 +2,6 @@ namespace Plang.Compiler.TypeChecker.Types { public class NamedTupleEntry { - public NamedTupleEntry() - { - } - - public NamedTupleEntry(string name, int fieldNo, PLanguageType type) - { - Name = name; - FieldNo = fieldNo; - Type = type; - } - public string Name { get; set; } public int FieldNo { get; set; } public PLanguageType Type { get; set; }