1+ #!/usr/bin/env python3
2+ import json
3+ import os
4+ from pathlib import Path
5+
6+ # Load JSON data
7+ json_path = "/Users/stephen/Projects/Projects-ExtGit/IronSheepProductionsLLC/Propeller2/P2-Language-Study/P2-Knowledge-Base/engineering/ingestion/external-inputs/from-pnut-ts/SPIN2-Language-Specification.json"
8+ with open (json_path ) as f :
9+ data = json .load (f )
10+
11+ # Output directory
12+ operator_dir = "/Users/stephen/Projects/Projects-ExtGit/IronSheepProductionsLLC/Propeller2/P2-Language-Study/P2-Knowledge-Base/engineering/knowledge-base/P2/language/spin2/operators"
13+
14+ # Map of missing operators to create (31 total)
15+ missing_operators = {
16+ 'BMASK' : {'filename' : 'op_BMASK.yaml' , 'description' : 'Create bit mask from bit count (also PASM2 instruction)' },
17+ 'DECOD' : {'filename' : 'op_DECOD.yaml' , 'description' : 'Decode bit position to mask (also PASM2 instruction)' },
18+ 'ENCOD' : {'filename' : 'op_ENCOD.yaml' , 'description' : 'Encode highest bit position (also PASM2 instruction)' },
19+ 'FABS' : {'filename' : 'op_FABS.yaml' , 'description' : 'Floating point absolute value' },
20+ 'FSQRT' : {'filename' : 'op_FSQRT.yaml' , 'description' : 'Floating point square root' },
21+ 'ONES' : {'filename' : 'op_ONES.yaml' , 'description' : 'Count number of 1 bits (also PASM2 instruction)' },
22+ 'QEXP' : {'filename' : 'op_QEXP.yaml' , 'description' : 'Quick exponential (also PASM2 instruction)' },
23+ 'QLOG' : {'filename' : 'op_QLOG.yaml' , 'description' : 'Quick logarithm (also PASM2 instruction)' },
24+ 'SQRT' : {'filename' : 'op_SQRT.yaml' , 'description' : 'Integer square root' },
25+ '|' : {'filename' : 'op_or.yaml' , 'description' : 'Bitwise OR' },
26+ '+//' : {'filename' : 'op_addmodulo.yaml' , 'description' : 'Unsigned remainder (modulo)' },
27+ 'FRAC' : {'filename' : 'op_FRAC.yaml' , 'description' : 'Calculate fraction' },
28+ 'SCA' : {'filename' : 'op_SCA.yaml' , 'description' : 'Scale value (also PASM2 instruction)' },
29+ 'SCAS' : {'filename' : 'op_SCAS.yaml' , 'description' : 'Scale value signed (also PASM2 instruction)' },
30+ 'ADDBITS' : {'filename' : 'op_ADDBITS.yaml' , 'description' : 'Add bit count to pin number' },
31+ 'ADDPINS' : {'filename' : 'op_ADDPINS.yaml' , 'description' : 'Add pin count to pin number' },
32+ '<.' : {'filename' : 'op_ltdot.yaml' , 'description' : 'Floating point less than comparison' },
33+ '<=.' : {'filename' : 'op_lteqdot.yaml' , 'description' : 'Floating point less than or equal comparison' },
34+ '<>.' : {'filename' : 'op_ltgtdot.yaml' , 'description' : 'Floating point inequality comparison' },
35+ '==.' : {'filename' : 'op_eqeqdot.yaml' , 'description' : 'Floating point equality comparison' },
36+ '>.' : {'filename' : 'op_gtdot.yaml' , 'description' : 'Floating point greater than comparison' },
37+ '>=.' : {'filename' : 'op_gteqdot.yaml' , 'description' : 'Floating point greater than or equal comparison' },
38+ 'NOT' : {'filename' : 'op_NOT.yaml' , 'description' : 'Logical NOT (also PASM2 instruction)' },
39+ 'AND' : {'filename' : 'op_AND.yaml' , 'description' : 'Logical AND (also PASM2 instruction)' },
40+ '^^' : {'filename' : 'op_xorxor.yaml' , 'description' : 'Logical XOR operator' },
41+ 'XOR' : {'filename' : 'op_XOR.yaml' , 'description' : 'Logical XOR (also PASM2 instruction)' },
42+ '||' : {'filename' : 'op_oror.yaml' , 'description' : 'Logical OR operator' },
43+ 'OR' : {'filename' : 'op_OR.yaml' , 'description' : 'Logical OR (also PASM2 instruction)' },
44+ '? :' : {'filename' : 'op_ternary.yaml' , 'description' : 'Ternary conditional operator' },
45+ ':=' : {'filename' : 'op_assign.yaml' , 'description' : 'Assignment operator' },
46+ ':=:' : {'filename' : 'op_swap.yaml' , 'description' : 'Swap operator' }
47+ }
48+
49+ # Get full operator data from JSON
50+ operator_map = {}
51+ for op in data .get ('operators' , []):
52+ symbol = op .get ('symbol' )
53+ if symbol :
54+ operator_map [symbol ] = op
55+
56+ # Create YAML files for missing operators
57+ created_count = 0
58+ for symbol , file_info in missing_operators .items ():
59+ filepath = os .path .join (operator_dir , file_info ['filename' ])
60+
61+ # Get operator data from JSON if available
62+ op_data = operator_map .get (symbol , {})
63+
64+ # Determine category
65+ category = op_data .get ('category' , 'Unknown' )
66+ if category == 'Unknown' :
67+ # Infer category from operator type
68+ if symbol in ['BMASK' , 'DECOD' , 'ENCOD' , 'ONES' , 'QEXP' , 'QLOG' , 'SCA' , 'SCAS' , 'NOT' , 'AND' , 'XOR' , 'OR' ]:
69+ category = 'Unary'
70+ elif symbol in ['FABS' , 'FSQRT' , 'SQRT' , 'FRAC' ]:
71+ category = 'Math'
72+ elif symbol in ['<.' , '<=.' , '<>.' , '==.' , '>.' , '>=.' ]:
73+ category = 'Float Compare'
74+ elif symbol in ['|' , '||' , '^^' ]:
75+ category = 'Bitwise'
76+ elif symbol in ['+//' ]:
77+ category = 'Arithmetic'
78+ elif symbol in ['ADDBITS' , 'ADDPINS' ]:
79+ category = 'Pin'
80+ elif symbol in ['? :' , ':=' , ':=:' ]:
81+ category = 'Assignment'
82+
83+ # Build YAML content
84+ yaml_content = f'''operator: "{ symbol } "
85+ type: operator
86+ description: |
87+ { file_info ['description' ]}
88+ category: { category } '''
89+
90+ # Add syntax if available
91+ if op_data .get ('syntax' ):
92+ yaml_content += f"\n syntax: \" { op_data ['syntax' ]} \" "
93+
94+ # Add examples if available
95+ if op_data .get ('examples' ):
96+ yaml_content += "\n examples:"
97+ for example in op_data ['examples' ]:
98+ yaml_content += f'\n - "{ example } "'
99+
100+ # Write the file
101+ with open (filepath , 'w' ) as f :
102+ f .write (yaml_content )
103+ created_count += 1
104+ print (f"Created: { file_info ['filename' ]} " )
105+
106+ print (f"\n Successfully created { created_count } operator YAML files" )
0 commit comments