Building a DSL with GraalVM (Devoxx PL)

B 	 	DSL	B 	 	DSL	
G VMG VM
M 	MM 	M
D 	S 	LD 	S 	L
https://pxhere.com/en/photo/1057524
SELECT			country,	
									COUNT(1)	AS	count
FROM					Customer	
GROUP	BY	country;
Given	my	account	has	a	balance	of	€	430	
When	I	receive	a	money	transfer	of	€	500	
Then	my	account	should	have	a	balance	of	€	930
Setup(	
				Boat	on	StartingShore,	
				Wolf	on	StartingShore,	
				Sheep	on	StartingShore,	
				Cabbage	on	StartingShore	
)	execute	(	
				Boat	move	Cabbage	to	StartingShore,	
				Boat	move	Sheep	to	DestinationShore,	
				Boat	move	None	to	StartingShore,	
				Boat	move	Cabbage	to	DestinationShore,	
				Boat	move	Sheep	to	StartingShore,	
				Boat	move	Wolf	to	DestinationShore,	
				Boat	move	None	to	StartingShore,	
				Boat	move	Sheep	to	DestinationShore	
)	
https://github.com/NRBPerdijk/dsl-for-the-dense/
+	+	>	+	+	+	+	+	[	<	+	>	-	]	+	+	+	+	+	+	+	+	[	<	+	+	+	+	+	+	>	-	]	<	.
BB
This	program	adds	the	numbers	2	and	5	
	
+	+																Store	the	number	2	in	the	first	slot	
>	+	+	+	+	+								Store	the	number	5	in	the	second	slot	
	
[																		Move	back	and	forth	between	first	and	second	slot	
	
				<	+	>	-								While	"moving"	ones	from	the	second	to	the	first	slot
]	
	
The	first	slot	now	has	'7'	in	it	but	we	need	to	output	the	ASCII	value	for	that
The	ASCII	value	is	48	higher	than	the	number	we	have	
	
The	second	slot	is	now	empty
It	will	act	as	a	counter	so	we	can	add	6	*	8	to	the	first	slot
+	+	+	+	+	+	+	+				Store	8	in	it	
	
[																		Again	move	back	and	forth	between	first	and	second	slot	
				<	+	+	+	+	+	+		while	adding	6	to	the	first	slot	
				>	-												before	reducing	the	counter	in	the	second	slot
]	
	
<	.																Go	back	to	the	first	slot	and	print	it
0 1 2 3 4 ... 29	997 29	998 29	999
2 5 3 0 0 0 0 0
↑
command meaning
+ increase	value
- decrease	value
. print	value	to	std	out
, read	one	byte	from	std	in
[ if	value	eq	0,	jump	after	matching	]
] if	value	neq	0,	jump	after	matching	[
< move	pointer	left
> move	pointer	right
https://www.thepinkhumanist.com/articles/330-life-of-alan-turing-examined-in-a-new-graphic-novel
https://commons.wikimedia.org/wiki/File:USA_tar_bubble_la_brea_CA.jpg
G VMG VM
“One	VM	to	rule	them	all
cc-by-sa/2.0	-	©	Lairich	Rig	-	https://www.geograph.org.uk/photo/3203827
Building a DSL with GraalVM (Devoxx PL)
BB
yapi.bf	calculating	15	digits	of	π
Runtime Average	time	(ms/op) Error
Java	HotSpot(TM)	64-Bit	Server	VM 53 ±	1
OpenJDK	GraalVM	CE	19.0.0 45 ±	1
All	tests	are	ran	on	an	2018	MacBook	Pro	with	2.6	GHz	Intel	Core	i7	and	16	GB	2400	MHz	DDR4.	The	machine	runs	macOS	Mojave	10.14.4	and	JDK	1.8.0_212.	Tests
measured	with	 .	Each	test	ran	5	times	with	5	warmup	iterations.jmh
BB
yapi.bf	calculating	45	digits	of	π
Runtime Average	time	(ms/op) Error
Java	HotSpot(TM)	64-Bit	Server	VM 207 ±	2
OpenJDK	GraalVM	CE	19.0.0 185 ±	3
All	tests	are	ran	on	an	2018	MacBook	Pro	with	2.6	GHz	Intel	Core	i7	and	16	GB	2400	MHz	DDR4.	The	machine	runs	macOS	Mojave	10.14.4	and	JDK	1.8.0_212.	Tests
measured	with	 .	Each	test	ran	5	times	with	5	warmup	iterations.jmh
TT
cc-by-sa/2.5	-	©	Darvin	DeShazer	-	https://mushroomobserver.org/2538
“open	source	library	for	building
programming	language	implementations
as	interpreters	for	self-modifying	Abstract
Syntax	Trees.
A 	S 	TA 	S 	T
ROOT
INCR_VAL INCR_VAL INCR_VAL INCR_VAL INCR_VAL JUMP INCR_VAL INCR_VAL
DECR_PTR INCR_VAL INCR_PTR DECR_VAL
(part	of	the	program	that	adds	5	and	2)
https://chrisseaton.com/rubytruffle/pldi17-truffle/pldi17-truffle.pdf
P 	EP 	E
Calculate	 	for	positive	integers
If	we	know	that	 ,	the	program	becomes	simpler:
x
n
f (x, n) =
⎧
⎩
⎨
⎪
⎪
1
,(f (x, 0.5 ∗ n))
2
x ∗ f (x, n − 1),
if n = 0
if n is	even
otherwise
n = 5
f (x) = x ∗ ( )x
2
2
BB
https://pxhere.com/en/photo/493605
BB
@Override
public	void	execute(final	VirtualFrame	frame)	{	
				final	int	currentValue	=	someCalculation();	
				doPrint(getContext().getOutput(),	(char)	currentValue);	
}	
	
@TruffleBoundary
private	void	doPrint(final	PrintWriter	out,	final	char	value)	{	
				out.print(value);	
				out.flush();	
}
SS
https://www.mammoet.com/cases/Tennet/
SS
@Specialization(guards	=	"b	 	0")	
public	double	divide(int	a,	int	b)	{	
				return	a	/	b;	
}
SS
@Specialization(rewriteOn	=	ArithmeticException.class)	
int	doAddNoOverflow(int	a,	int	b)	{	
				return	Math.addExact(a,	b);	
}	
	
@Specialization
long	doAddWithOverflow(int	a,	int	b)	{	
				return	a	+	b;	
}	
execute(Integer.MAX_VALUE	-	1,	1)	 	doAddNoOverflow(Integer.MAX_VALUE	-	1,	1)	
	
execute(Integer.MAX_VALUE				,	1)	 	doAddNoOverflow(Integer.MAX_VALUE,	1)	
																																					throws	ArithmeticException	
																																		 	doAddWithOverflow(Integer.MAX_VALUE,	1)	
	
execute(Integer.MAX_VALUE	-	1,	1)	 	doAddWithOverflow(Integer.MAX_VALUE	-	1,	1)
W 	 	G VM	 	 	 	 	JVMW 	 	G VM	 	 	 	 	JVM
function	abs	(int	i)		
				if	(	 	we	saw	only	positive	integers	in	the	input	 	)	{	
								return	i;	
				}	else	{		
								transferToInterpreterAndInvalidate;	
								return	i	<	0	?	 i	:	i;	
				}	
}
B 	 	 	 	TB 	 	 	 	T
Building a DSL with GraalVM (Devoxx PL)
LL
Converts	a	sequence	of	characters	into	a	sequence	of	tokens.
PP
Converts	a	sequence	of	tokens	into	(hierarchical)	data	structure.
P 	 	L 	P 	 	L 	
1.	Write	some	regular	expressions
2.	Use	a	parser	generator	(like	 )ANTLR
cc-by-nc/2.5	-	©	Randall	Munroe	-	https://www.xkcd.com/1171/
I 	 	I
G VM	UG VM	U
GraalVM	comes	with	the	GraalVM	Updater	(gu)
Use	gu	to	install	components,	such	as	language	packs	or	tools.
e.g.	gu install native-image
gu	-L	install	brainfuck component-0.1-SNAPSHOT.jar
CC
Distribute	your	language	implementation	as	a	component:
$	tree	
.	
├──	META-INF	
│			├──	MANIFEST.MF	
│			├──	permissions	
│			└──	symlinks	
└──	jre	
				└──	languages	
								└──	bf	
												├──	bin	
												│			└──	bf	
												├──	brainfuck.jar	
												└──	launcher	
																└──	bf launcher.jar	
	
6	directories,	6	files
U 	 	 	U 	 	 	
1.	Prepare	source	code
2.	Prepare	GraalVM	polyglot	context
3.	Evaluate	the	source	code
input	=	"+	+	>	+	+	+	+	+	[	<	+	>	-	]	+	+	+	+	+	+	+	+	[	<	+	+	+	+	+	+	>	-	]	<	.";	
source	=	Source.newBuilder("bf",	input,	"user	input").build();	
output	=	new	ByteArrayOutputStream();	
context	=	Context.newBuilder("bf").out(output).build();	
context.eval(source);	
System.out.println(output.toString());
TT
“Implementing	your	own	language	using
GraalVM	will	not	only	give	you	high
performance.	More	importantly,	it	allows
your	language	to	connect	with	the	rich
tooling	provided	by	the	GraalVM	ecosystem.
https://www.graalvm.org/docs/graalvm-as-a-platform/
https://pxhere.com/en/photo/1067853
DD
Start	the	launcher	with	--inspect
Debugger listening on port 9229.
To start debugging, open the following URL in Chrome:
chrome-devtools://devtools/bundled/js_app.html?ws=127.0.0.1:9229/77b52d12-53f20cb0ab35
Building a DSL with GraalVM (Devoxx PL)
O 	O 	
The	Truffle	framework	has	an	Instrument	API	to	write	other	tools,	e.g.	code
coverage	measurement,	profilers.
1.	Source	code-related	events
2.	Allocation	events
3.	Language	runtime	and	thread	creation	events
4.	Application	execution	events
W 	W 	
Yes,	you	can	run	any	language	with	GraalVM.
...	but	it	may	take	some	time.
It's	certainly	fun
...	and	it	might	even	be	profitable.
T 	T 	
You	don't	need	to	write	a	parser	yourself
(and	maybe	you	don't	want	to,	either)
Take	time	to	think	about	the	AST
using	a	wrong	structure	leads	to	hard-to-track	bugs
refactoring	it	later	is	very	hard	and	time-consuming
Q	 	AQ	 	A
Sample	code:	
Please	help	conference	organisers:	don't	forget	to	rate
this	talk	in	the	Devoxx	app!
http://bit.ly/brainfuck-jvm
@mthmulders  #DevoxxPL  #GraalDSL
1 of 42

Recommended

Building a DSL with GraalVM (javaBin online) by
Building a DSL with GraalVM (javaBin online)Building a DSL with GraalVM (javaBin online)
Building a DSL with GraalVM (javaBin online)Maarten Mulders
221 views44 slides
Building a DSL with GraalVM (Oracle Groundbreaker APAC Virtual Tour) by
Building a DSL with GraalVM (Oracle Groundbreaker APAC Virtual Tour)Building a DSL with GraalVM (Oracle Groundbreaker APAC Virtual Tour)
Building a DSL with GraalVM (Oracle Groundbreaker APAC Virtual Tour)Maarten Mulders
128 views43 slides
Building a DSL with GraalVM (CodeOne) by
Building a DSL with GraalVM (CodeOne)Building a DSL with GraalVM (CodeOne)
Building a DSL with GraalVM (CodeOne)Maarten Mulders
160 views41 slides
Building a DSL with GraalVM (VoxxedDays Luxembourg) by
Building a DSL with GraalVM (VoxxedDays Luxembourg)Building a DSL with GraalVM (VoxxedDays Luxembourg)
Building a DSL with GraalVM (VoxxedDays Luxembourg)Maarten Mulders
239 views41 slides
Infrastructural challenges of a fast-pace startup by
Infrastructural challenges of a fast-pace startupInfrastructural challenges of a fast-pace startup
Infrastructural challenges of a fast-pace startupDevOps Braga
96 views38 slides
Consul ou comment bien tirer sur l’élastique by
 Consul ou comment bien tirer sur l’élastique Consul ou comment bien tirer sur l’élastique
Consul ou comment bien tirer sur l’élastiqueNicolas Ledez
167 views160 slides

More Related Content

More from Maarten Mulders

What's cooking in Maven? (Devoxx FR) by
What's cooking in Maven? (Devoxx FR)What's cooking in Maven? (Devoxx FR)
What's cooking in Maven? (Devoxx FR)Maarten Mulders
173 views25 slides
Making Maven Marvellous (Devnexus) by
Making Maven Marvellous (Devnexus)Making Maven Marvellous (Devnexus)
Making Maven Marvellous (Devnexus)Maarten Mulders
149 views13 slides
Making Maven Marvellous (Java.il) by
Making Maven Marvellous (Java.il)Making Maven Marvellous (Java.il)
Making Maven Marvellous (Java.il)Maarten Mulders
146 views13 slides
Making Maven Marvellous (JavaZone) by
Making Maven Marvellous (JavaZone)Making Maven Marvellous (JavaZone)
Making Maven Marvellous (JavaZone)Maarten Mulders
90 views13 slides
Dapr: Dinosaur or Developer's Dream? (v1) by
Dapr: Dinosaur or Developer's Dream? (v1)Dapr: Dinosaur or Developer's Dream? (v1)
Dapr: Dinosaur or Developer's Dream? (v1)Maarten Mulders
132 views42 slides
Dapr: Dinosaur or Developer Dream? (J-Fall) by
Dapr: Dinosaur or Developer Dream? (J-Fall)Dapr: Dinosaur or Developer Dream? (J-Fall)
Dapr: Dinosaur or Developer Dream? (J-Fall)Maarten Mulders
138 views42 slides

More from Maarten Mulders(20)

What's cooking in Maven? (Devoxx FR) by Maarten Mulders
What's cooking in Maven? (Devoxx FR)What's cooking in Maven? (Devoxx FR)
What's cooking in Maven? (Devoxx FR)
Maarten Mulders173 views
Making Maven Marvellous (Devnexus) by Maarten Mulders
Making Maven Marvellous (Devnexus)Making Maven Marvellous (Devnexus)
Making Maven Marvellous (Devnexus)
Maarten Mulders149 views
Making Maven Marvellous (Java.il) by Maarten Mulders
Making Maven Marvellous (Java.il)Making Maven Marvellous (Java.il)
Making Maven Marvellous (Java.il)
Maarten Mulders146 views
Dapr: Dinosaur or Developer's Dream? (v1) by Maarten Mulders
Dapr: Dinosaur or Developer's Dream? (v1)Dapr: Dinosaur or Developer's Dream? (v1)
Dapr: Dinosaur or Developer's Dream? (v1)
Maarten Mulders132 views
Dapr: Dinosaur or Developer Dream? (J-Fall) by Maarten Mulders
Dapr: Dinosaur or Developer Dream? (J-Fall)Dapr: Dinosaur or Developer Dream? (J-Fall)
Dapr: Dinosaur or Developer Dream? (J-Fall)
Maarten Mulders138 views
React in 40 minutes (Voxxed Days Romania) by Maarten Mulders
React in 40 minutes (Voxxed Days Romania) React in 40 minutes (Voxxed Days Romania)
React in 40 minutes (Voxxed Days Romania)
Maarten Mulders93 views
React in 50 minutes (Bucharest Software Craftsmanship Community) by Maarten Mulders
React in 50 minutes (Bucharest Software Craftsmanship Community)React in 50 minutes (Bucharest Software Craftsmanship Community)
React in 50 minutes (Bucharest Software Craftsmanship Community)
Maarten Mulders244 views
React in 50 Minutes (JNation) by Maarten Mulders
 React in 50 Minutes (JNation)  React in 50 Minutes (JNation)
React in 50 Minutes (JNation)
Maarten Mulders143 views
SSL/TLS for Mortals (Oracle Groundbreaker EMEA Virtual Tour) by Maarten Mulders
SSL/TLS for Mortals (Oracle Groundbreaker EMEA Virtual Tour)SSL/TLS for Mortals (Oracle Groundbreaker EMEA Virtual Tour)
SSL/TLS for Mortals (Oracle Groundbreaker EMEA Virtual Tour)
Maarten Mulders114 views
SSL/TLS for Mortals (UtrechtJUG) by Maarten Mulders
SSL/TLS for Mortals (UtrechtJUG)SSL/TLS for Mortals (UtrechtJUG)
SSL/TLS for Mortals (UtrechtJUG)
Maarten Mulders202 views
SSL/TLS for Mortals (Lockdown Lecture) by Maarten Mulders
SSL/TLS for Mortals (Lockdown Lecture)SSL/TLS for Mortals (Lockdown Lecture)
SSL/TLS for Mortals (Lockdown Lecture)
Maarten Mulders122 views
React in 50 Minutes (OpenValue) by Maarten Mulders
React in 50 Minutes (OpenValue) React in 50 Minutes (OpenValue)
React in 50 Minutes (OpenValue)
Maarten Mulders162 views
React in 50 Minutes (DevNexus) by Maarten Mulders
React in 50 Minutes (DevNexus) React in 50 Minutes (DevNexus)
React in 50 Minutes (DevNexus)
Maarten Mulders114 views
Building web applications with React (Jfokus) by Maarten Mulders
Building web applications with React (Jfokus)Building web applications with React (Jfokus)
Building web applications with React (Jfokus)
Maarten Mulders203 views

Recently uploaded

FOSSLight Community Day 2023-11-30 by
FOSSLight Community Day 2023-11-30FOSSLight Community Day 2023-11-30
FOSSLight Community Day 2023-11-30Shane Coughlan
7 views18 slides
Top-5-production-devconMunich-2023.pptx by
Top-5-production-devconMunich-2023.pptxTop-5-production-devconMunich-2023.pptx
Top-5-production-devconMunich-2023.pptxTier1 app
9 views40 slides
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium... by
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...Lisi Hocke
35 views124 slides
Dapr Unleashed: Accelerating Microservice Development by
Dapr Unleashed: Accelerating Microservice DevelopmentDapr Unleashed: Accelerating Microservice Development
Dapr Unleashed: Accelerating Microservice DevelopmentMiroslav Janeski
15 views29 slides
Programming Field by
Programming FieldProgramming Field
Programming Fieldthehardtechnology
6 views9 slides
How Workforce Management Software Empowers SMEs | TraQSuite by
How Workforce Management Software Empowers SMEs | TraQSuiteHow Workforce Management Software Empowers SMEs | TraQSuite
How Workforce Management Software Empowers SMEs | TraQSuiteTraQSuite
6 views3 slides

Recently uploaded(20)

FOSSLight Community Day 2023-11-30 by Shane Coughlan
FOSSLight Community Day 2023-11-30FOSSLight Community Day 2023-11-30
FOSSLight Community Day 2023-11-30
Shane Coughlan7 views
Top-5-production-devconMunich-2023.pptx by Tier1 app
Top-5-production-devconMunich-2023.pptxTop-5-production-devconMunich-2023.pptx
Top-5-production-devconMunich-2023.pptx
Tier1 app9 views
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium... by Lisi Hocke
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...
Lisi Hocke35 views
Dapr Unleashed: Accelerating Microservice Development by Miroslav Janeski
Dapr Unleashed: Accelerating Microservice DevelopmentDapr Unleashed: Accelerating Microservice Development
Dapr Unleashed: Accelerating Microservice Development
Miroslav Janeski15 views
How Workforce Management Software Empowers SMEs | TraQSuite by TraQSuite
How Workforce Management Software Empowers SMEs | TraQSuiteHow Workforce Management Software Empowers SMEs | TraQSuite
How Workforce Management Software Empowers SMEs | TraQSuite
TraQSuite6 views
Ports-and-Adapters Architecture for Embedded HMI by Burkhard Stubert
Ports-and-Adapters Architecture for Embedded HMIPorts-and-Adapters Architecture for Embedded HMI
Ports-and-Adapters Architecture for Embedded HMI
Burkhard Stubert33 views
How to build dyanmic dashboards and ensure they always work by Wiiisdom
How to build dyanmic dashboards and ensure they always workHow to build dyanmic dashboards and ensure they always work
How to build dyanmic dashboards and ensure they always work
Wiiisdom14 views
Generic or specific? Making sensible software design decisions by Bert Jan Schrijver
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisions
Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated... by TomHalpin9
Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated...Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated...
Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated...
TomHalpin96 views
Automated Testing of Microsoft Power BI Reports by RTTS
Automated Testing of Microsoft Power BI ReportsAutomated Testing of Microsoft Power BI Reports
Automated Testing of Microsoft Power BI Reports
RTTS10 views
Quality Engineer: A Day in the Life by John Valentino
Quality Engineer: A Day in the LifeQuality Engineer: A Day in the Life
Quality Engineer: A Day in the Life
John Valentino7 views

Building a DSL with GraalVM (Devoxx PL)