Skip to content

Instantly share code, notes, and snippets.

@rummik
Last active April 10, 2019 04:44
Show Gist options
  • Save rummik/9726111 to your computer and use it in GitHub Desktop.
Save rummik/9726111 to your computer and use it in GitHub Desktop.
Random Dimond code examples
#**
* For statement definition
*
* @example
* for { $i=0; $i<20; $i++ } {
* ...
* }
*#
define { Function, Function } 'for' {
{ $expression, $body } = $$~arguments
{ $initial, $condition, $each } = $expression
$expression~scope = $body~scope = $$~stack[-2]~scope
$initial()
{
unless $condition() {
$body()
$each()
$self()
}
}
}
#**
* If statement
*
* @example
* if $condition {
* ...
* }
*#
define { ???, Function } 'if' {
{ $condition, $body } = $$~arguments
$condition && $body()
}
#**
* Unless statement
*
* @example
* unless $condition {
* ...
* }
*#
define { ???, Function } 'unless' {
{ $condition, $body } = $$~arguments
$condition || $body()
}
@anoxic
Copy link

anoxic commented Apr 10, 2019

some assumed operators (from the examples):

=	assignment
<	lt
||	or, short-circuiting
&&	and, short-circuiting
++	incr
$	variable indicator
$$	some kind of magic var referring to hidden stuff
~	property access
[]	list access

roughly, syntax might be (guessing and making a few things up):

comment    : '#*' /.*/ '*#'
operator   : '+' | '-' | '*' | '/' | '%' | '<' | '>' | '||' | '&&' | '++'
scalars    : <typespec> | <number> | <string> | <list>
typespec   : /[A-Z][a-z0-9_]+/ | '???'
number     : /-?[0-9]+\\.[0-9]+/ | /-?[0-9]+/
string     : "'" /[^']/ "'"
list       : '{' <expression> [ /,|\n/ <expression> ] '}'
word       : /[A-Za-z0-9_]+/
variable   : '$' <word> | '$$'
fncall     : /[a-z][A-Za-z0-9]+/ <expression>+ <list>
vfncall    : <variable> '(' [<expression> [',' <expression>]] ')'
expression : <scalars> | <variable> | <fncall>
assignment : <variable> | <list> '=' <expression>
propaccess : <expression> '~' <word>
listaccess : <expression> '[' <expression> ']'

i'm basically guessing, "define" is actually a function, and if a function's last argument is a list, you don't need parens to call it. either that, or only functions bound to a variable need the parens.

the last item in a function is what it returns. don't know how early return looks or if it's a thing

i think things inside { .. } are a list that isn't evaluated right away, and you could delimit with comma or newline. i didn't put it above, but maybe a list inside ( .. ) is one whose contents are evaluated right away

also not sure, although i make a specific syntax for assignment, it could actually be where '=' is just an operator that modifies the scope/state and isn't a higher part of the syntax like this

i don't get what the difference between ; and , is here. on the for example you have ; but everywhere else with the same pattern is ,

probably i use totally different words than you would to describe this. also made a bunch of assumptions. anyway, is it anywhere near correct?

@anoxic
Copy link

anoxic commented Apr 10, 2019

possible way to define lambda parameters

{ -> ( Integer, String )
    ...
}

or with named parameters

{ -> ( foo:Integer, bar:(String, null) ;; baz:Boolean )
    ...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment