CounterClockWise
Jump to navigation
Jump to search
- Not to be confused with Counter clockwise.
CounterClockWise is an esolang(generalized computational model) created by islptng.
Literally, there is counters and clocks, and you can make wise programs. The title is unrelated to rotation.
Syntax
A program consists of counters. To express a counter:
CounterName : InitialValue = IterateExpression
InitialValue should be an integer.
Prefix := | <Prefix><Prefix> | *<Number> | /<Number> IterateExpression := | -<IterateExpression> | <IterateExpression>+<IterateExpression> | <IterateExpression>-<IterateExpression> | <Prefix><CounterName> | <Number> | (<IterateExpression>?<IterateExpression>:<IterateExpression>.<IterateExpression>)
(a?b!c.d): If a is positive, evaluate b; If zero, evaluate c; else evaluate d.
You can have a number (prefixed with a slash or an asterisk) before a name of the counter, for multiplication and integer division(floor).
We run in steps, for each step, we update each counter at the same time.
Examples
Collatz sequence
collatz = 54 : (collatz - *2/2collatz ? *3collatz+1 ! /2collatz .)
Interpreter
I need help.
There is an compiler by cycwin and AI assistant.
import re import copy def transform_expression(expr): """Convert Prefix expression to infix""" # Recursive conversion function def transform(e): # 1. Handle /nVar → (var//n) e = re.sub(r'/(\d+)([a-zA-Z_]\w+)', r'(\2//\1)', e) #print(e,"debug") # 2. Handle *nVar → (n*var) e = re.sub(r'\*(\d+)([a-zA-Z_0-9\(\)/\*]+)', r'(\1'+'*'+r'\2)', e) return e # First convert non-conditional parts expr = transform(expr) # Handle conditional expressions (a?b!c.d) while True: new_expr = re.sub( r'\((.*?)\?(.*?)!(.*?)\.(.*?)\)', lambda m: ( f'({transform(m.group(2)) or 0} if {transform(m.group(1)) or 0}>0 ' f'else {transform(m.group(3)) or 0} if {transform(m.group(1)) or 0}==0 ' f'else {transform(m.group(4)) or 0})' ), expr ) if new_expr == expr: break expr = new_expr '''# Final verification if any(op in expr for op in ['*2(', '*3(', '/2(']): raise ValueError(f"Residual prefix expression: {expr}")''' return expr # Complete compiler class CCWCompiler: def __init__(self, program): self.program = program def compile(self): code = [ "import copy", "objects = {}", "backup = {}", "" ] # Parse counters for line in self.program.strip().split('\n'): if not line.strip(): continue if match := re.match(r'^(\w+)\s*:\s*(-?\d+)\s*=\s*(.*)$', line.strip()): name, init, expr = match.groups() code.append('objects["'+name+'"] = '+init) # Generate execution code code += [ "while True:", " print('current state:', objects)", " backup = copy.deepcopy(objects)", " updates = {}" ] # Transform expressions for line in self.program.strip().split('\n'): if not line.strip(): continue if match := re.match(r'^(\w+)\s*:\s*(-?\d+)\s*=\s*(.*)$', line.strip()): name, _, expr = match.groups() final_expr = transform_expression(expr) code.append(f' updates["{name}"] = "{final_expr}"') code += [ " for name, expr in updates.items():", " try:", " objects[name] = eval(expr, {}, backup)", " except Exception as e:", ' print(f"! Error {name}: {e}")', "", ' if input("continue? (y/n): ").lower() != "y":', " break", "", 'print("Final results:", objects)' ] return '\n'.join(code) # Execution example program = """ collatz : 2 = (collatz - *2/2collatz ? *3collatz+1 ! /2collatz .) c : 0 = c + 1 k : 1 = 1 """ print("=== Compile result ===") compiler = CCWCompiler(program) compiled_code = compiler.compile() print(compiled_code) print("\n=== Perform verification ===") exec_globals = {'copy': copy} exec(compiled_code, exec_globals)