Add defun/loop macro

pull/70/head
Stephan Seitz 2021-04-03 02:14:47 +07:00
parent a9d8b569ea
commit fbf1f672c5
6 changed files with 41264 additions and 11101 deletions

@ -23,7 +23,7 @@ module.exports = grammar(clojure, {
name: 'commonlisp',
extras: ($, original) => [...original, $.block_comment],
conflicts: (_, original) => [...original],
conflicts: ($, original) => [...original, [$.for_clause]],
rules: {
block_comment: _ => token(seq('#|', repeat(choice(/[^|]/, /\|[^#]/)), '|#')),
@ -56,6 +56,38 @@ module.exports = grammar(clojure, {
repeat(choice(field('value', $._form), $._gap)),
field('close', ")")),
_for_part: $ => seq(optional($._gap), choice('in', 'across', 'being', 'using', /being the (hash-key[s]?|hash-value[s]?) in/, 'below', 'from', 'to', 'upto', 'downto', 'downfrom', 'on', 'by', 'then'),
optional($._gap), $._form),
accumulation_verb: _ => /(collect|append|nconc|count|sum|maximize|minimize)(ing)?/,
for_clause: $ => seq(choice('for', 'and'), optional($._gap), field('variable', $.sym_lit),
$._for_part, optional($._for_part)),
with_clause: $ => prec.left(seq('with', optional($._gap), $._form, optional($._gap), "=", optional($._gap), $._form)),
do_clause: $ => prec.left(seq('do', optional($._gap), $._form)),
condition_clause: $ => prec.left(seq(choice('when', 'if', 'unless', 'always', 'thereis', 'never'), optional($._gap), $._form)),
accumulation_clause: $ => prec.left(seq($.accumulation_verb, optional($._gap), $._form, optional($._gap), optional(seq('into', optional($._gap), $._form)))),
termination_clause: $ => prec.left(seq(choice('finally', 'return', 'initially'), optional($._gap), $._form)),
loop_clause: $ =>
seq(choice(
$.for_clause,
$.do_clause,
$.accumulation_clause,
$.condition_clause,
$.with_clause,
$.termination_clause,
)),
loop_macro: $ =>
seq(field('open', "("),
optional($._gap),
'loop',
repeat(choice($.loop_clause, $._gap)),
field('close', ")")),
defun_keyword: _ => choice('defun', 'defmacro'),
defun_header: $ =>
@ -73,15 +105,11 @@ module.exports = grammar(clojure, {
_bare_list_lit: $ =>
choice(prec(PREC.SPECIAL, $.defun),
prec(PREC.SPECIAL, $.loop_macro),
seq(field('open', "("),
repeat(choice(field('value', $._form), $._gap)),
field('close', ")"))),
_first_form: $ =>
choice(
prec(PREC.SPECIAL, $.defun_header),
$._form),
_form: $ =>
choice(// atom-ish
$.num_lit,

@ -895,7 +895,15 @@
"value": 2,
"content": {
"type": "SYMBOL",
"name": "_defun"
"name": "defun"
}
},
{
"type": "PREC",
"value": 2,
"content": {
"type": "SYMBOL",
"name": "loop_macro"
}
},
{
@ -1711,7 +1719,7 @@
]
}
},
"_defun": {
"defun": {
"type": "SEQ",
"members": [
{
@ -1768,6 +1776,505 @@
}
]
},
"_for_part": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_gap"
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "in"
},
{
"type": "STRING",
"value": "across"
},
{
"type": "STRING",
"value": "being"
},
{
"type": "STRING",
"value": "using"
},
{
"type": "PATTERN",
"value": "being the (hash-key[s]?|hash-value[s]?) in"
},
{
"type": "STRING",
"value": "below"
},
{
"type": "STRING",
"value": "from"
},
{
"type": "STRING",
"value": "to"
},
{
"type": "STRING",
"value": "upto"
},
{
"type": "STRING",
"value": "downto"
},
{
"type": "STRING",
"value": "downfrom"
},
{
"type": "STRING",
"value": "on"
},
{
"type": "STRING",
"value": "by"
},
{
"type": "STRING",
"value": "then"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_gap"
},
{
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
"name": "_form"
}
]
},
"accumulation_verb": {
"type": "PATTERN",
"value": "(collect|append|nconc|count|sum|maximize|minimize)(ing)?"
},
"for_clause": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "for"
},
{
"type": "STRING",
"value": "and"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_gap"
},
{
"type": "BLANK"
}
]
},
{
"type": "FIELD",
"name": "variable",
"content": {
"type": "SYMBOL",
"name": "sym_lit"
}
},
{
"type": "SYMBOL",
"name": "_for_part"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_for_part"
},
{
"type": "BLANK"
}
]
}
]
},
"with_clause": {
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "with"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_gap"
},
{
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
"name": "_form"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_gap"
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": "="
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_gap"
},
{
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
"name": "_form"
}
]
}
},
"do_clause": {
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "do"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_gap"
},
{
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
"name": "_form"
}
]
}
},
"condition_clause": {
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "when"
},
{
"type": "STRING",
"value": "if"
},
{
"type": "STRING",
"value": "unless"
},
{
"type": "STRING",
"value": "always"
},
{
"type": "STRING",
"value": "thereis"
},
{
"type": "STRING",
"value": "never"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_gap"
},
{
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
"name": "_form"
}
]
}
},
"accumulation_clause": {
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "accumulation_verb"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_gap"
},
{
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
"name": "_form"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_gap"
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "into"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_gap"
},
{
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
"name": "_form"
}
]
},
{
"type": "BLANK"
}
]
}
]
}
},
"termination_clause": {
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "finally"
},
{
"type": "STRING",
"value": "return"
},
{
"type": "STRING",
"value": "initially"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_gap"
},
{
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
"name": "_form"
}
]
}
},
"loop_clause": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "for_clause"
},
{
"type": "SYMBOL",
"name": "do_clause"
},
{
"type": "SYMBOL",
"name": "accumulation_clause"
},
{
"type": "SYMBOL",
"name": "condition_clause"
},
{
"type": "SYMBOL",
"name": "with_clause"
},
{
"type": "SYMBOL",
"name": "termination_clause"
}
]
}
]
},
"loop_macro": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "open",
"content": {
"type": "STRING",
"value": "("
}
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_gap"
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": "loop"
},
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "loop_clause"
},
{
"type": "SYMBOL",
"name": "_gap"
}
]
}
},
{
"type": "FIELD",
"name": "close",
"content": {
"type": "STRING",
"value": ")"
}
}
]
},
"defun_keyword": {
"type": "CHOICE",
"members": [
@ -1836,23 +2343,6 @@
"value": "A"
}
]
},
"_first_form": {
"type": "CHOICE",
"members": [
{
"type": "PREC",
"value": 2,
"content": {
"type": "SYMBOL",
"name": "defun_header"
}
},
{
"type": "SYMBOL",
"name": "_form"
}
]
}
},
"extras": [
@ -1861,7 +2351,11 @@
"name": "block_comment"
}
],
"conflicts": [],
"conflicts": [
[
"for_clause"
]
],
"precedences": [],
"externals": [],
"inline": [],

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

@ -1,15 +1,7 @@
(loop for dim-idx in xyz below x
for y from 0
for letter in (list 'x 'y 'z) in 2
collect x)
(ql.foo:quickload "cl-project")
(defun add (a b)
(+ a b ))
(add 8 9)
(defun foo (instruction)
(etypecase instruction
(number instruction)
(cons (car instruction))
(t instruction)))
;(foo '(0 . 1))
(loop for x below 3 in 2
do (format t "~A~%" x))

@ -82,3 +82,21 @@ Arrays
---
(source (vec_lit (num_lit)) (vec_lit (array_dimension (num_lit)) (list_lit (list_lit (num_lit) (num_lit) (num_lit)))))
==========================================
Loop Macro
==========================================
(loop for dim-idx in xyz
for letter in (list 'x 'y 'z)
collect (let ((current-range (nth dim-idx iteration-ranges)))
`(, (get-counter-symbol dim-idx)
(+ ,(range-start current-range)
(* ,(range-step current-range)
,(case letter
(x '(+ thread-idx-x (* block-idx-x block-dim-x)))
(y '(+ thread-idx-y (* block-idx-y block-dim-y)))
(z '(+ thread-idx-z (* block-idx-z block-dim-z)))))))))
---
(source (list_lit (loop_macro (loop_clause (for_clause (sym_lit) (sym_lit))) (loop_clause (for_clause (sym_lit) (list_lit (sym_lit) (quoting_lit (sym_lit)) (quoting_lit (sym_lit)) (quoting_lit (sym_lit))))) (loop_clause (accumulation_clause (accumulation_verb) (list_lit (sym_lit) (list_lit (list_lit (sym_lit) (list_lit (sym_lit) (sym_lit) (sym_lit)))) (syn_quoting_lit (list_lit (unquoting_lit (list_lit (sym_lit) (sym_lit))) (list_lit (sym_lit) (unquoting_lit (list_lit (sym_lit) (sym_lit))) (list_lit (sym_lit) (unquoting_lit (list_lit (sym_lit) (sym_lit))) (unquoting_lit (list_lit (sym_lit) (sym_lit) (list_lit (sym_lit) (quoting_lit (list_lit (sym_lit) (sym_lit) (list_lit (sym_lit) (sym_lit) (sym_lit))))) (list_lit (sym_lit) (quoting_lit (list_lit (sym_lit) (sym_lit) (list_lit (sym_lit) (sym_lit) (sym_lit))))) (list_lit
(sym_lit) (quoting_lit (list_lit (sym_lit) (sym_lit) (list_lit (sym_lit) (sym_lit) (sym_lit)))))))))))))))))