Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Generalization of Numbers
Generalization of Numbers [message #1767023] Thu, 29 June 2017 15:18 Go to next message
Marco Ullrich is currently offline Marco UllrichFriend
Messages: 14
Registered: December 2016
Location: Bayreuth
Junior Member
Hello everyone,

I'm working on a programming language for beginners and want to generalize integers and floatingpoint numbers to one datatype (instead of Integer and Double) so beginners don't have to bother with multiple types for numbers.

I decided to use java.lang.Numbers for this purpose (because it already generalizes java.lang.Integer and java.lang.Double).

I used https://koehnlein.blogspot.de/2011/07/extending-xbase.html and https://eclipse.org/Xtext/documentation/203_builddsl.html for a first approach.

But it seems that my extension methods for Number are ignored, because the operators are not working.

Here a minimal example, using the domainmodel dsl:
entity e1{	
	op op2 ():Number {			 
		var Number n = 1
		n = 1 - 1 //Doesnt work: "Couldn't resolve reference to JvmIdentifiableElement '-'."
        	n = (1 as Double) - 1 // Works, bacause of cast, uses DoubleExtensions				
		return 1 // Works
	}
}


These are my customisations so far:
All XNumberLiteral are of type Number
class MyTypeComputer extends XbaseTypeComputer {	
        override void _computeTypes(XNumberLiteral object, ITypeComputationState state) {		
		val LightweightTypeReference result = getTypeForName(typeof(Number), state);
		state.acceptActualType(result);
	}
}


For the first approach, the compiler simply print out the XNumberLiteral -value
class MyCompiler extends XbaseCompiler {
	override _toJavaExpression(XNumberLiteral expr, ITreeAppendable b) {		
                 b.append(expr.value)
	}
}


Providing some operators for the type Number
class NumberExtensions {
    def public static Number operator_minus(Number x, Number y) {
         return x - y;
     }
    //.....
}


Including the NumberExtensions
class MyImplicitlyImportedFeatures extends ImplicitlyImportedFeatures {
	override protected getExtensionClasses() {
		(super.getExtensionClasses() + #[NumberExtensions])
			.toList
	}
}


Thanks in advance for your help and best regards,
Marco
Re: Generalization of Numbers [message #1767025 is a reply to message #1767023] Thu, 29 June 2017 15:28 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Hi,

is NumberExtensions on the classpath of the dmodel file?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Generalization of Numbers [message #1767034 is a reply to message #1767025] Thu, 29 June 2017 18:10 Go to previous messageGo to next message
Marco Ullrich is currently offline Marco UllrichFriend
Messages: 14
Registered: December 2016
Location: Bayreuth
Junior Member
DOH!
That was too obvious... now it works, thank you! (:
Can you explain to me, what would be the best way to ship the library with my dsl?
I don't want the end-user of my dsl to add this file to their classpath manually.

[Updated on: Thu, 29 June 2017 18:12]

Report message to a moderator

Re: Generalization of Numbers [message #1767035 is a reply to message #1767034] Thu, 29 June 2017 18:47 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Maybe you can do something similar like the "add xtend library" QuickFix in xtend

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:X-Text Linking Services
Next Topic:Xtext Generator Chaining
Goto Forum:
  


Current Time: Tue Mar 19 06:43:09 GMT 2024

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

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

Back to the top