Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Classes - part1: parse classes #219

Closed
riederm opened this issue Jul 30, 2021 · 4 comments · Fixed by #225
Closed

Support Classes - part1: parse classes #219

riederm opened this issue Jul 30, 2021 · 4 comments · Fixed by #225
Assignees

Comments

@riederm
Copy link
Collaborator

riederm commented Jul 30, 2021

Parse class POUs and get an approriate representation in the AST.

Classes ...

  • have an identifier
  • declare multiple variable declaration blocks
  • dont have a body
  • can have mulitple methods

Methods...

  • have an identifier
  • can have a visibility modifer (PUBLIC, PRIVATE, INTERNAL, PROTECTED [default])
  • may declare additional variable delcaration blocks
  • can declare a return-type (like functions do)
  • have a body
CLASS COUNTER
	VAR
		CV: UINT;
		Max: UINT := 1000;
	END_VAR
	
	METHOD PUBLIC Up: UINT
		VAR_INPUT INC: UINT; END_VAR
		VAR_OUTPUT QU: BOOL; END_VAR
		
		IF CV <= Max - INC THEN
			CF := CF + INC;
			QU := FALSE;
		ELSE
			QU := TRUE;
		END_IF
		UP := CV;
	END_METHOD
	
	METHOD PUBLIC Up5: UINT
		VAR_OUTPUT QU: BOOL; END_VAR
		Up5 := THIS.UP(INC := 5, QU => QU);
	END_METHOD
END_CLASS
@ulmer-a ulmer-a self-assigned this Jul 30, 2021
@ulmer-a
Copy link
Collaborator

ulmer-a commented Aug 3, 2021

I am documenting my progress here to simpilfy writing documentation for this afterwards. The grammer being accepted by RuSTy will look like this.

CLASS [ABSTRACT|FINAL] <ClassName>
	[USING <Namespace1>, <Namespace2>, ... ]       // not yet implemented
	[EXTENDS [...[<Type2>.]]ClassName]                     // not yet implemented
	[IMPLEMENTS <Interface1>, <Interface2>, ...]      // not yet implemented
	
	
	// Any number of VAR / VAR blocks
	VAR [CONSTANT] [RETAIN|NON_RETAIN] [PUBLIC|PROTECTED|PRIVATE|INTERNAL]
		<VarName> : ... ;
	END_VAR
	
	// Any number of method declarations like this:
	METHOD [PUBLIC|PROTECTED|PRIVATE|INTERNAL] [FINAL|ABSTRACT] [OVERRIDE] <MethodName> [: RETURN_TYPE]
		
		// Any number of VAR blocks
		VAR [CONSTANT] [PUBLIC|PROTECTED|PRIVATE|INTERNAL]
			<VarName> : ... ;
		END_VAR
		
		/// Any number of IO_VAR declarations^
		[VAR_INPUT|VAR_OUTPUT|VAR_INOUT] [RETAIN|NON_RETAIN]
			...
		END_VAR
		
		VAR_TEMP
			...
		END_VAR
		
		<function_body>
	
	END_METHOD

END_CLASS

@ulmer-a
Copy link
Collaborator

ulmer-a commented Aug 5, 2021

Regarding Variable Blocks inside a class declaration:

  • VAR_EXTERNAL and VAR_EXTERNAL_CONSTANT are used to capture global variables so that they are accessible from within the class. This is hardly ever used and therefore not implemented for now, especially since global variables can be referenced without capturing anyway.
  • VAR blocks are member variables. The CONSTANT keyword may be specified to make variables read-only.
  • On VAR blocks, an access modifier (Public, Protected, Private, Internal) may be specified. If not specified, Protected will be default.
  • VAR blocks marked with RETAIN or NON_RETAIN are member variables with special behaviour with respect to a reset. When nothing was specified, NON_RETAIN will be the default.

In short: VAR...END_VAR blocks are the only allowed type of variable blocks inside a class declaration and they may be marked with the discussed keywords in that respective order.

@ulmer-a
Copy link
Collaborator

ulmer-a commented Aug 6, 2021

Regarding Variable Blocks inside a method declaration:

  • VAR_INPUT, VAR_OUTPUT and VAR_INOUT can be used to specify arguments to the method. Only the keywords RETAIN or NON_RETAIN may be specified on those.
  • VAR and VAR_TEMP blocks can be used to declare temporary variables. Their use is equivalent.

@ulmer-a
Copy link
Collaborator

ulmer-a commented Aug 6, 2021

The parser will parse all variable blocks equally, so the validity of specified keywords and initializers, etc. has to be checked during semantic analysis. However, specifying a IO blocks inside a class declaration will yield a parser error.

@ghaith ghaith added this to the Object Oriented ST milestone Aug 9, 2021
@ghaith ghaith moved this to Done in Next Jan 9, 2023
@ghaith ghaith added this to Next Jan 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants