Designing Dialects for Control and Communication

What is a Dialect?

Dialect is a term for a small language, or Domain-Specific-Languages (DSLs), that achieves a certain purpose. What does this mean, practically? Well, let's look at a use-case:

Dice

Let's say I need to calculate a lot of diverse dice rolls, I want to make a list of the number and type of dice that I want to calculate, such as below:

dice: [10 d10 4 d6]

Thiings we notice: First, it's readable; and second, there are no function calls for each number of dice. This is a little "language" I have created for this problem. I need to be able to parse this language and the rules will look something like this:

dice-rule: [
	some [integer! word!]
]

This rule will tell whether the language matches the language or not:

>> parse dice dice-rule
== true

We can even extend the "language" to include a die without a number in front of it:

dice: [10 d8 d10 50 d6 d100]
dice-rule: [
	some [
		word! 
	|
		integer! word!
	]
]

And the language is still parse-able.

>> parse dice dice-rule
== true

However, it won't calculate the dice rolls yet. We can add some extra commands to do so:

dice-rule: [
	(total: 0)
	some [
		copy die word!
		(total: total + roll 1 get die/1)
	|
		copy n integer!
		copy die word!
		(total: total + roll n/1 get die/1)
	]
]

Now, all we need to do is to define the roll function to add up the number of random rolls for each word and define the values for each die word! Here is my implementation of a calculate function that does the above.

random/seed now/precise

d2: 2
d4: 4
d6: 6
d8: 8
d12: 12
d20: 20
d100: 100

calculate: function [b] [
	dice-rule: [
		(total: 0)
		some [
			copy die word!
			(total: total + roll 1 get die/1)
		|
			copy n integer!
			copy die word!
			(total: total + roll n/1 get die/1)
		]
	]

	roll: func [times [integer!] die [integer!]] [
		result: 0
		repeat i times [result: result + random die]
		result
	]

	parse b dice-rule
	total
]

repeat i 400 [ 
	print calculate [10 d10 4 d6]
]

How to Get Started

Grab a pen or a text editor and write out some test cases. What do you want to happen and when?

My Projects

Dialects