Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Generalization of Numbers
Generalization of Numbers [message #1767023] Thu, 29 June 2017 11:18 Go to next message
Eclipse UserFriend
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 11:28 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

is NumberExtensions on the classpath of the dmodel file?
Re: Generalization of Numbers [message #1767034 is a reply to message #1767025] Thu, 29 June 2017 14:10 Go to previous messageGo to next message
Eclipse UserFriend
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 14:12] by Moderator

Re: Generalization of Numbers [message #1767035 is a reply to message #1767034] Thu, 29 June 2017 14:47 Go to previous message
Eclipse UserFriend
Maybe you can do something similar like the "add xtend library" QuickFix in xtend
Previous Topic:X-Text Linking Services
Next Topic:Xtext Generator Chaining
Goto Forum:
  


Current Time: Fri May 16 15:18:39 EDT 2025

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

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

Back to the top