-
-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Macros? #42
Comments
yay! |
How about this syntax? GENSYM_COUNT=0
macro gensym(var) # create an unique name
GENSYM_COUNT+=1
local name<-create-ast name "__gensym_name_$GENSYM_COUNT"
return value :{local ~var=~name}
macro swap(x y) # swap the values of the given variables
gensym tmp
return code
~tmp=~x
~x=~y
~y=~tmp
a=1
b=2
swap a b
echo $a $b #> 2 1 Suggestions/criticism as always are welcome. Explanation: First off, macros manipulate data, not strings, so in order to avoid having users have to do all the things I did with add(a b)
return value $(math a+b)
x<-add 1 2
echo $x #> 3 Which would translate to Bash as something like: add() {
local a="${1}" b="${2}"
printf -v "$__return_value_add" '%s' $((${a}+${b}))
}
__return_value_add=__return_value_0 add 1 2
x=$__return_value_0
echo $x The second bit of syntax is the code literal,
ast:make code local ''
ast:make __ast_child_0 assign 'x' $value
ast:push-child $code $__ast_child_0
return value :{
block
~tmp=~x
~x=~y
~y=~tmp
} The rest I imagine is straight forward:
creates a macro Just to give some perspective on why manipulating data is better with add_echo() {
echo $((${1} + ${2}))
}
So having syntax for this strategy of data passing would be nice in general. |
Alternatively instead of another type of assignment x<-f x y we could have a "value substitution" syntax x=$[f x y] Which has a number of advantages like allowing more than one to be passed to a command and being more "shell-like", but I wonder if it wouldn't be confusing. If this requires more discussion I will open another issue, although macros require either functionality to be easy to use. |
last time I've used macros was with C (long time ago). |
An AST based compiler allows lisp-like macros to be implemented.
It would be very handy in implementing the remaining and future powscript extensions, as well as dealing with the fact that
eval
doesn't work with powscript code by introducing alternatives.yay/nay?
The text was updated successfully, but these errors were encountered: