Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » User defined functions with builtin functions
User defined functions with builtin functions [message #1846952] Sun, 10 October 2021 09:33 Go to next message
Charalampos Alexopoulos is currently offline Charalampos AlexopoulosFriend
Messages: 13
Registered: July 2012
Junior Member
Hi
I am trying to make grammar that allows both user defined functions and language builtin function like in the example code bellow.
function mf(i)=i*3);
a=2*mf(2)
b=sin(45)*4;
c=mf(2)+sin(45);

sin(45) is a builtin function

I can do either a or b but not c

I simplify my grammar as i could . I try several thinks but i have to admit that my understanding of the Xtext grammar is limited to say the least. So my tries were more or less guesswork.
grammar org.xtext.example.test.Test with org.eclipse.xtext.common.Terminals

generate test "http://www.xtext.org/example/test/Test"


Package:
	Statements;
	
Statements:
	 (statements+=Statement)+
;

Statement:
	Assignment";"
	|NamedFunctionDefinition
;


Number:
	intPart=INT ("." decimalPart=INT)? |
	"." decimalPart=INT
;


Expr returns Expression: ExprLet

;

ExprLet returns Expression:
	(lets+=LetClause)* in=ExprIndexedHead
;

ExprIndexedHead returns Expression:
	arr=ExprElvis ("[" index=ExprElvis "]")?
;

ExprElvis returns Expression:
	cond=ExprOr ("?" if=ExprOr ":" else=ExprOr)?
;

ExprOr returns Expression:
	ExprAnd ({Expression.left=current} op='||' right=ExprAnd)*
;

ExprAnd returns Expression:
	ExprRel ({Expression.left=current} op='&&' right=ExprRel)*
;

ExprRel returns Expression:
	ExprAdd ({Expression.left=current} op=('>='|'>'|'=='|'!='|'<='|'<') right=ExprAdd)*
;

ExprAdd returns Expression:
	ExprMul ({Expression.left=current} op=('+' | '-') right=ExprMul)*
;

ExprMul returns Expression:
	ExprUni ({Expression.left=current} op=('*' | '/' | '%') right=ExprUni)*
;

ExprUni returns Expression:
	{Expression} (op=('-' | '!' | '+'))? right=ExprAtomic
;

ExprAtomic:
	FunctionCall |
	"(" Expr ")" |
	{NumberLiteral} val=Number |
	{StringLiteral} val=STRING |
	VariableReference 
;

VariableReference:
	{BuiltinVar} "$" name=ID |
	{UserVar} name=ID
;

LetClause:
	{LetClause} "let""("ass=Assignments?")"
;

NamedFunctionDefinition:
	"function" name=ID "(" params=Parameters? ")=" body=Expr ";"  
;

BuiltinFunctions:
	name=NamedBuiltin
;

// Works with user defined functios
//FunctionCall:
//	function=[NamedFunctionDefinition|ID] "(" args=Arguments? ")"
//;

// Works with builtin functions
//FunctionCall:
//	function=BuiltinFunctions "(" args=Arguments? ")"
//;

// This doesn't work
FunctionCall:
	function=[BuiltinFunctions|NamedFunctionDefinition|ID] "(" args=Arguments? ")"
;

Parameters:
	parameters+=Parameter ("," parameters+=Parameter)*
;

AssignmentOrExpr:
	var=VariableReference ("=" tail=Expr) |
	val=Expr
;

Parameter:
	AssignmentOrExpr // TODO: Name from Assignment
;

Arguments:
	arguments+=Argument ("," arguments+=Argument)*
;

Argument:
	Expr | Assignment
;

Assignments:
	assignments+=Assignment ("," assignments+=Assignment)*
;

Assignment:
	name=VariableReference "=" val=Expr
;

NamedBuiltin:
	'abs' | 'sin' | 'cos' | 'tan'
;


//



Re: User defined functions with builtin functions [message #1846955 is a reply to message #1846952] Sun, 10 October 2021 11:27 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

You don't say exactly what doesn't work, but I suspect that the problem is that for built-ins FunctionCall.function is a magic ID, whereas for user-defined it is a reference.

You could make your reference anarchically polymorphic, but given that the built-in functions might expand, I suggest introducing declarations for them possibly redirecting to an alternate syntax that specifies java.lang.Math.sin, or my.builtins.Sin as the actual built-in implementation. You then only need polymorphism between a builtin and user-defined definitions. The built-in definitions might be hosted via an imported module.

Regards

Ed Willink
Re: User defined functions with builtin functions [message #1847101 is a reply to message #1846955] Thu, 14 October 2021 15:29 Go to previous message
Charalampos Alexopoulos is currently offline Charalampos AlexopoulosFriend
Messages: 13
Registered: July 2012
Junior Member
Thank you for your help.
Previous Topic:XText + XSemantics + Maven
Next Topic:Default Cross-References
Goto Forum:
  


Current Time: Fri Apr 19 05:24:54 GMT 2024

Powered by FUDForum. Page generated in 0.01920 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top