Skip to main content



      Home
Home » Modeling » TMF (Xtext) » User defined functions with builtin functions
User defined functions with builtin functions [message #1846952] Sun, 10 October 2021 05:33 Go to next message
Eclipse UserFriend
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 07:27 Go to previous messageGo to next message
Eclipse UserFriend
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 11:29 Go to previous message
Eclipse UserFriend
Thank you for your help.
Previous Topic:XText + XSemantics + Maven
Next Topic:Default Cross-References
Goto Forum:
  


Current Time: Wed Jul 02 07:43:20 EDT 2025

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

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

Back to the top