- commit
- 102cb4821c7d33b02d08e4f0e67eb94f9757f278
- parent
- 19bc70ddb4d67757857645c7b32a791d4b449c09
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2024-08-09 12:42
add support for arrays
Diffstat
| M | README.md | 2 | ++ |
| M | xipd/format.py | 4 | ++++ |
| M | xipd/parser.py | 6 | ++++++ |
| M | xipd/renderer.py | 3 | +++ |
4 files changed, 15 insertions, 0 deletions
diff --git a/README.md b/README.md
@@ -103,6 +103,8 @@ of the following patterns: 103 103 - **Connection**: Create a connection between two nodes. (e.g. ``foo:1 -> 104 104 `dac~` ``) 105 105 - **Include**: Include code from another file (e.g. `include "../foo.xipd"`) -1 106 - **Array**: Create a named array that can store data, e.g. samples (e.g. -1 107 `array "mysample"`) 106 108 - **Start a function**: (e.g. `foo(a, b) {`) 107 109 - **End a function**: Each function must end with `}` 108 110 - **Return**: Each function must return an expression. This should be the
diff --git a/xipd/format.py b/xipd/format.py
@@ -20,6 +20,8 @@ def pd2dot(pd): 20 20 elif line.startswith('#X obj') or line.startswith('#X msg'): 21 21 out.append(f' {index};') 22 22 index += 1 -1 23 elif line.startswith('#X array'): -1 24 index += 1 23 25 out.append('}') 24 26 return '\n'.join(out) 25 27 @@ -49,6 +51,8 @@ def apply_positions(pd, positions): 49 51 lines.append(' '.join(parts)) 50 52 index += 1 51 53 else: -1 54 if line.startswith('#X array'): -1 55 index += 1 52 56 lines.append(line) 53 57 return ''.join(l + '\r\n' for l in lines) 54 58
diff --git a/xipd/parser.py b/xipd/parser.py
@@ -183,6 +183,11 @@ class Parser: 183 183 path, s = self.parse_str(s) 184 184 return ('include', path[1]), s 185 185 -1 186 def parse_array(self, s): -1 187 _, s = self.parse_re(s, r'array *') -1 188 name, s = self.parse_str(s) -1 189 return ('array', name[1]), s -1 190 186 191 def parse_return(self, s): 187 192 _, s = self.parse_re(s, r'return *') 188 193 expr, s = self.parse_expr(s) @@ -191,6 +196,7 @@ class Parser: 191 196 def parse_line(self, s): 192 197 ast, s = self.parse_any(s, [ 193 198 self.parse_include, -1 199 self.parse_array, 194 200 self.parse_return, 195 201 self.parse_connect, 196 202 self.parse_assign,
diff --git a/xipd/renderer.py b/xipd/renderer.py
@@ -109,6 +109,9 @@ class Renderer: 109 109 with open(_path) as fh: 110 110 ast = self.parser.parse_file(fh) 111 111 self.render_with_scope(ast, scope, _path) -1 112 elif stmt[0] == 'array': -1 113 self._print(f'X array {stmt[1]} 0 float') -1 114 self.create_node() 112 115 elif stmt[0] == 'return': 113 116 _, expr = stmt 114 117 return self.expr_to_ref(expr, scope)