Skip to content

Add support for memory operations (CLR, FIL, ROL, ROR)#11

Merged
ckrause merged 4 commits intomainfrom
copilot/add-memory-operation-support
Oct 19, 2025
Merged

Add support for memory operations (CLR, FIL, ROL, ROR)#11
ckrause merged 4 commits intomainfrom
copilot/add-memory-operation-support

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Oct 19, 2025

Overview

This PR implements support for four LODA memory operations: clr, fil, rol, and ror. These operations were previously missing from the Python runtime but are documented in the LODA language reference and implemented in the C++ version.

Changes

Operation Types

Added three new operation types to the Operation.Type enum (CLR already existed but was not implemented):

  • FIL (34): Fill memory range
  • ROL (35): Rotate left
  • ROR (36): Rotate right

Memory Operation Functions

Implemented four memory operation functions in loda/runtime/operations.py:

  • clr(mem, start, length): Clears (sets to zero) all memory cells in the specified range
  • fil(mem, start, length): Fills all cells in the range with the value at the start position
  • rol(mem, start, length): Rotates values in the range left by one position with wrap-around
  • ror(mem, start, length): Rotates values in the range right by one position with wrap-around

All operations support positive, negative, and zero-length ranges following the C++ semantics:

  • Positive length: [start, start+length)
  • Negative length: [start+length+1, start+1) (backward range)
  • Zero length: Empty range (no-op)

Interpreter Updates

Extended the interpreter to handle memory operations:

  • Added operation handling in the main execution loop
  • Implemented memory range limit checking to prevent unbounded operations
  • Proper address resolution for target operands using get_address=True

Example Usage

from loda.lang import Program
from loda.runtime import Interpreter

# Fill operation: set cells $1-$4 to the value in $1
program = Program("""
mov $1,42
fil $1,4
""")
# Result: $1=42, $2=42, $3=42, $4=42

# Rotate left: shift values with wrap-around
program = Program("""
mov $1,10
mov $2,20
mov $3,30
rol $1,3
""")
# Result: $1=20, $2=30, $3=10 (rotated left)

Testing

Added comprehensive test suite (tests/test_memory_operations.py) with 17 tests covering:

  • Positive and negative length ranges
  • Zero-length operations (no-ops)
  • Sparse memory handling
  • Indirect addressing
  • Memory limit enforcement for all operations

All existing tests continue to pass, confirming backward compatibility.

Security

  • Memory limits are properly enforced via max_memory parameter to prevent DoS attacks through unbounded memory allocation
  • CodeQL security scan found 0 alerts
  • All edge cases are properly handled with validation

Fixes #[issue_number]

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • https://api.github.com/repos/loda-lang/loda-cpp/contents/src/lang
    • Triggering command: curl -s REDACTED (http block)
  • loda-lang.org
    • Triggering command: curl -s REDACTED (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Add support for memory operations</issue_title>
<issue_description>LODA supports some new memory operations: clr, fil, rol, ror.

Extend the LODA Python runtime to support the operation types.

They are documented here: https://loda-lang.org/reference/#memory-operations

You can find C++ code here:
https://github.com/loda-lang/loda-cpp/blob/main/src/eval/memory.cpp

Caution: ensure a limit for the maximum memory range size.</issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #10


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 2 commits October 19, 2025 04:44
Co-authored-by: ckrause <840744+ckrause@users.noreply.github.com>
Co-authored-by: ckrause <840744+ckrause@users.noreply.github.com>
Copilot AI changed the title [WIP] Add support for new memory operations in LODA Add support for memory operations (CLR, FIL, ROL, ROR) Oct 19, 2025
Copilot AI requested a review from ckrause October 19, 2025 04:51
Copy link
Copy Markdown
Contributor

@ckrause ckrause left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Please check my comment.

Comment thread loda/lang/operation.py Outdated
CLR = 31 # clear
SEQ = 32 # sequence
DBG = 33 # debug
FIL = 34 # fill
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move the three new operations up after CLR.

@ckrause
Copy link
Copy Markdown
Contributor

ckrause commented Oct 19, 2025

@copilot Please check my comments.

@ckrause ckrause merged commit b2a180f into main Oct 19, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for memory operations

2 participants