Replies: 2 comments 1 reply
-
I agree that it can be nice to allow such clean simple syntaxes. But in fact, civet as it stands comes extremely close to supporting the syntax shown in the initial example: see this working example in the playground. For those not clicking through to the playground, it sets up functions so that the current civet syntax
does precisely what's described as desired in that first example. Thus, the only difference from the proposed syntax and the working syntax is the presence of the verb
compile to
is a parse error. But because it is a parse error, it seems there would be an opportunity: I would propose that if a line ends with a function or an unclosed function application (even with no comma) and the next line is further indented, then the accumulation of successive lines as (additional) arguments until a dedent is initiated, just as it currently is when a line ends with an unclosed function application to one or more arguments and a comma. What else could the otherwise unnecessary extra indentation mean? With that change, the exact syntax and semantics of the first example could be achieved. And with the ease of braceless object construction and arrow functions, the other examples can be achieved pretty closely -- e.g., just at the cost of an extra Do those here more experienced think that parsing and compiling
to |
Beta Was this translation helpful? Give feedback.
-
This proposal is now effectively implemented by indented function call arguments as hinted at above. In particular, the original example can be written as: program := onFlagClicked
repeat 10,
move 10
turn 15 (which differs in a single comma) function onFlagClicked(...block)
new FlagClicked(block)
function repeat(count, ...args)
new Repeat(count, ...args)
function move(dist)
new MoveSteps(dist)
function turn(angle)
new Turn(dist) Another difference is there isn't any automatic construction of arrays. But if you want arrays, I think bulleted arrays are a good solution: program := onFlagClicked
. repeat 10,
. move 10
. turn 15 |
Beta Was this translation helpful? Give feedback.
-
These are some attempts (from Discord discussions with MystPI) at adding some metaprogramming features without full macros, to make it easier to build natural-looking domain-specific languages within Civet.
Indented Blocks as Array Arguments
Suppose you want to be able to build trees like the following:
Examples include ASTs, e.g. Babel code building or otherwise representing programs that you'd like to export to a language or system (e.g. Scratch).
But we'd like to do it with syntax like this:
I think an interesting approach would be to enable a special kind of function that accepts an indented block, which gets turned into an array argument, as if it were an array literal (each line becomes an item, commas can separate items). Something like this code:
Presumably
arg[]
would have to be the last argument.I'm not sure it's best to put the annotation on the argument. If we'd like to
import
functions like this, we might want a different sort offunction
, like we haveoperator
now.Indented Blocks as Function Arguments
What else might we want to do with indented arguments? Maybe Ruby's
&block
parameters. Here's a possible example:I'm less clear on how to name the argument, though. It's hard to compete with the existing syntax:
Perhaps something like the following?
Even without named arguments, though, you could do some cool things with reactivity, like this:
This is also a setting where we might want multiple arguments to be automatically wrapped in functions, so you can control if and when each argument gets evaluated. This seems harder to
import
, but you could imagine multiple&
annotations on arguments. For example:Again it'd be nice to have notation for the
&condition
to take an argument... but all I can think of is regular arrow functions.Beta Was this translation helpful? Give feedback.
All reactions