Editing Programs

Editing your own code to run in the virtual machine.

Editing Programs

A design decision was made early on to not have an in place editor. Perhaps this was wrong, but it is the way it is and the only way to edit a CMa program with the UI is with the build in Edit command. This command creates a modal pop up dialog with a text area to edit the code in and a button to parse it. Once the code is complete you can choose to load it into the virtual machine and begin executing it.

Contents

Code Editor

The Edit command will open up the modal Program Editor dialog, and if there is already a program in the VM it will show the current set of source code (including comments). Pressing the Parse button will parse the code using the very simple parser in this application. The instructions it finds are displayed to the right in a small table. Labels in the first column and the instruction and its argument in the next. Pressing the Load Program button will close the dialog and load the program in the VM. Pressing Close (or the x) will simply dismiss the dialog without making changes to the VM.

Code Editor

WIn general whitespace is ignored in by the parser. Each line in the file is expected to contain at most one instruction. All text after a semicolon is considered a comment. Instructions can have multiple arguments, however all but one instruction (at this time) have at most on one argument. Arguments are separated by a space (not a tab).

A line can begin with a label followed by a colon. This label can be used as an instruction reference as in the case of a jump instruction, or as a program address reference (the physical address of the instruction in the Program Store) and used in an instruction like loada.

This implementation of the virtual machine has some extensions to make it useful when examining concepts and examples in the text. If a comment includes the text #MEMSET then the rest of the comment is parsed for special commands to initialize main memory with specific values. This makes it easier to try out the examples in the text where an address space is given prior to executing some code.

The statement after the #MEMSET keyword the parser looks for a command of the form:

{label} : S[{address}] = {value}

Where the values inside the curley braces are replaced by a string label, absolute memory address and a value of type int, float or char.

For example the following comment commands:

; #MEMSET x: S[5] = 10
; #MEMSET pi: S[6] = 3.14
; #MEMSET letter: S[7] = 'A'

Will initialize the memory with the following values, and any labels will be automatically added as a Note.

Main Memory Initialized by special instructions in comments.

Load URL

You can pre-load a source file by passing a reference to some raw source code in CMa after the hash tag in the main VM URL. For example suppose the application was hosted at https://jconallen.github.io/CMa/. Calling this URL directly will only load the application. If you specify the URL of a program with the query parameter src and optionally the size of main memory with the query parameter mem, then the app will attempt to load that as CMa source and parse it. In order for this to work the site hosting the code must allow Cross Origin Resource Sharing (CORS).

Fortunately if you use GitHub to store your files, you can specify a GitHub link to your CMa code to load in the machine when the page opens. All you need to do is URL encode the URL to your source and put it after a hash in the link to the application. For example the solution to problem 2.11 #1 of the text is placed in the GitHub hosted file: https://github.com/jconallen/CMa/blob/master/2.11.1.cma however because this link includes a lot of HTML, we need the Raw version of the file (CMa code only), which you get by pressing the Raw button. It is this URL that you place after the hash in the app url.

The URL https://jconallen.github.io/CMa?src=https://raw.githubusercontent.com/jconallen/CMa/master/2.11.1.cma will load the app, hosted on GitHub and then load the CMa program 2.11.1.cam. This link can be passed around in an email or specified in a web page like this: Problem 2.11 #1