Skip to content

Commit 29a2426

Browse files
author
hartsantler
committed
starting on new go syntax for array and map types [2]string(x,y) and map[string]int{'x':x, 'y':y}
1 parent cca265f commit 29a2426

1 file changed

Lines changed: 41 additions & 1 deletion

File tree

pythonjs/typedpython.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ def transform_source( source, strip=False ):
3232

3333
for line in source.splitlines():
3434
a = []
35+
hit_go_typedef = False
36+
gotype = None
37+
3538
for i,char in enumerate(line):
3639
nextchar = None
3740
j = i+1
@@ -40,7 +43,31 @@ def transform_source( source, strip=False ):
4043
if nextchar.strip(): break
4144
j += 1
4245

43-
if a and char in __whitespace:
46+
if a and char==']' and j==i+1 and nextchar in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ':
47+
assert '[' in a
48+
gotype = []
49+
b = a.pop()
50+
while b != '[':
51+
gotype.append(b)
52+
b = a.pop()
53+
gotype.reverse()
54+
gotype = ''.join(gotype)
55+
if not gotype:
56+
a.append('__go__array__(')
57+
elif gotype.isdigit():
58+
a.append('__go__arrayfixed__(%s,' %gotype)
59+
else:
60+
a.append('__go__map__(%s,' %gotype)
61+
hit_go_typedef = True
62+
63+
elif hit_go_typedef and char=='(':
64+
a.append(')<<(')
65+
hit_go_typedef = False
66+
elif hit_go_typedef and char=='{':
67+
a.append(')<<{')
68+
hit_go_typedef = False
69+
70+
elif a and char in __whitespace:
4471
b = ''.join(a)
4572
b = b.strip()
4673
if b in types and nextchar != '=':
@@ -351,6 +378,19 @@ def wrapper(a:int, chan c:int):
351378
case x = <- b:
352379
y += x
353380
381+
## in go becomes: []string{x,y,z}
382+
## becomes: __go__array__(string) << (x,y,z)
383+
a = []string(x,y,z)
384+
385+
## in go becomes: [3]int{x,y,z}
386+
## becomes: __go__arrayfixed__(3, string) << (x,y,z)
387+
a = [3]int(x,y,z)
388+
389+
## in go becomes: map[string]int{x,y,z}
390+
## becomes: __go__map__(string, int) << {'x':x, 'y':y, 'z':z}
391+
a = [string]int{
392+
"x":x, "y":y, "z":z
393+
}
354394
'''
355395

356396
if __name__ == '__main__':

0 commit comments

Comments
 (0)