From 9256bea446092733eb8f7349dc5f0610712e39f0 Mon Sep 17 00:00:00 2001 From: NeilFraser Date: Mon, 25 May 2026 11:53:04 +0200 Subject: [PATCH] Add single 'list' variable. --- Blockly/framework/List-test.agc | 33 ++++++++++++++++ Blockly/framework/List.agc | 18 +++++++++ Blockly/framework/Main.agc | 1 + Blockly/framework/Tests.agc | 1 + Blockly/html/agc/blocks/lists.js | 46 ++++++++++++++++++++++ Blockly/html/agc/generator/generator.js | 20 +++++----- Blockly/html/agc/generator/lists.js | 33 ++++++++++++++++ Blockly/html/agc/index.html | 51 ++++++++++++++++++------- 8 files changed, 180 insertions(+), 23 deletions(-) create mode 100644 Blockly/framework/List-test.agc create mode 100644 Blockly/framework/List.agc create mode 100644 Blockly/html/agc/blocks/lists.js create mode 100644 Blockly/html/agc/generator/lists.js diff --git a/Blockly/framework/List-test.agc b/Blockly/framework/List-test.agc new file mode 100644 index 0000000..ee54d66 --- /dev/null +++ b/Blockly/framework/List-test.agc @@ -0,0 +1,33 @@ +# Unit tests for the List functions. + +# TEST: SET + + CA NUM4 # list[0] -> 4 + TCR PUSH + CA NUM0 + TCR PUSH + TCR LS-SET + CA NUM2 # list[1] -> 2 + TCR PUSH + CA NUM1 + TCR PUSH + TCR LS-SET + + CA NUM4 # Test that list[0] == 4 + TCR PUSH + CA LIST + TCR PUSH + TCR TS-EQUAL + + CA NUM2 # Test that list[1] == 2 + TCR PUSH + CA NUM1 + COM + INDEX A + CA LIST + TCR PUSH + TCR TS-EQUAL + +# Blockly will normally define list. +# But for tests, it otherwise wouldn't be defined. +LIST = 3777 diff --git a/Blockly/framework/List.agc b/Blockly/framework/List.agc new file mode 100644 index 0000000..c9f971b --- /dev/null +++ b/Blockly/framework/List.agc @@ -0,0 +1,18 @@ +# List functions. + +# Set. +# Stack arguments: +# Value. +# Index. +# Uses L register. +LS-SET EXTEND + QXCH QPOP + TCR POP # Pop the index. + COM + TS L + TCR POP # Pop the value. + INDEX L + TS LIST + EXTEND + QXCH QPOP + RETURN diff --git a/Blockly/framework/Main.agc b/Blockly/framework/Main.agc index 1271861..1a2b25f 100644 --- a/Blockly/framework/Main.agc +++ b/Blockly/framework/Main.agc @@ -98,6 +98,7 @@ END CA A # Code modules. $Boolean.agc +$List.agc $Math.agc $Print.agc $Random.agc diff --git a/Blockly/framework/Tests.agc b/Blockly/framework/Tests.agc index 555bcb0..e6cd97a 100644 --- a/Blockly/framework/Tests.agc +++ b/Blockly/framework/Tests.agc @@ -2,6 +2,7 @@ # Execute the tests. $Boolean-test.agc +$List-test.agc $Math-test.agc $Random-test.agc $Print-test.agc diff --git a/Blockly/html/agc/blocks/lists.js b/Blockly/html/agc/blocks/lists.js new file mode 100644 index 0000000..d9c339d --- /dev/null +++ b/Blockly/html/agc/blocks/lists.js @@ -0,0 +1,46 @@ +/** + * @fileoverview List blocks for Blockly. + */ +'use strict'; + +Blockly.defineBlocksWithJsonArray([ + // Block for list item setter. + { + "type": "list_set", + "message0": "set list at %1 to %2", + "args0": [ + { + 'type': 'input_value', + 'name': 'INDEX', + 'check': 'Number', + }, + { + 'type': 'input_value', + 'name': 'VALUE', + 'check': 'Number', + }, + ], + "previousStatement": null, + "nextStatement": null, + "inputsInline": true, + "style": "list_blocks", + "helpUrl": null, + "tooltip": "Set the value in the list at the specified location.", + }, + // Block for list item getter. + { + "type": "list_get", + "message0": "get list at %1", + "args0": [ + { + 'type': 'input_value', + 'name': 'INDEX', + 'check': 'Number', + }, + ], + "output": null, + "style": "list_blocks", + "helpUrl": null, + "tooltip": "Get the value in the list at the specified location.", + }, +]); diff --git a/Blockly/html/agc/generator/generator.js b/Blockly/html/agc/generator/generator.js index cc98d8a..164f37e 100644 --- a/Blockly/html/agc/generator/generator.js +++ b/Blockly/html/agc/generator/generator.js @@ -72,16 +72,18 @@ AgcGenerator.init = function(workspace) { } // Declare all of the variables. - if (defvars.length) { - // Octal 4000 is just beyond the end of AGC's erasable memory. - // Declare variables starting from the end of memory and working downwards. - let memoryAddress = 0o4000; - this.definitions_['variables'] = ''; - for (const varName of defvars) { - memoryAddress--; - this.definitions_['variables'] += varName + '\t=\t' + memoryAddress.toString(8) + '\n'; - } + // Octal 4000 is just beyond the end of AGC's erasable memory. + // Declare variables starting from the end of memory and working downwards. + let memoryAddress = 0o4000; + this.definitions_['variables'] = ''; + for (const varName of defvars) { + memoryAddress--; + this.definitions_['variables'] += varName + '\t=\t' + memoryAddress.toString(8) + '\n'; } + // Final variable is the list. + memoryAddress--; + this.definitions_['variables'] += 'LIST\t=\t' + memoryAddress.toString(8) + '\n'; + this.isInitialized = true; }; diff --git a/Blockly/html/agc/generator/lists.js b/Blockly/html/agc/generator/lists.js new file mode 100644 index 0000000..1053088 --- /dev/null +++ b/Blockly/html/agc/generator/lists.js @@ -0,0 +1,33 @@ +/** + * @fileoverview Generating AGC assembly for list blocks. + */ +'use strict'; + +AgcGenerator['list_get'] = function(block) { + // List item getter. + const argument0 = AgcGenerator.valueToCode(block, 'INDEX') || + AgcGenerator.default0; + const code = ` +${argument0} +\tCOM +\tINDEX\tA +\tCA\tLIST +`; + return code; +}; + +AgcGenerator['list_set'] = function(block) { + // List item setter. + const argument0 = AgcGenerator.valueToCode(block, 'INDEX') || + AgcGenerator.default0; + const argument1 = AgcGenerator.valueToCode(block, 'VALUE') || + AgcGenerator.default0; + const code = ` +${argument1} +\tTCR\tPUSH +${argument0} +\tTCR\tPUSH +\tTCR\tLS-SET +`; + return code; +}; diff --git a/Blockly/html/agc/index.html b/Blockly/html/agc/index.html index 334c114..a22279a 100644 --- a/Blockly/html/agc/index.html +++ b/Blockly/html/agc/index.html @@ -14,6 +14,7 @@ --> + @@ -21,6 +22,7 @@ + @@ -71,6 +73,33 @@

Blockly AGC