pub struct SymbolTable { /* private fields */ }
Expand description

The symbol table stores and resolves symbols (labels and variables) to their associated addresses or values.

List of predefined symbols

Virtual Registers

SymbolValue
R00
R11
R1515

Input/Output

SymbolValue
SCREEN16384
KBD24576

Reserved

SymbolValue
SP0
LCL1
ARG2
THIS3
That4

Example

Basic Usage

let mut table = SymbolTable::new();
table.set("value", 42).unwrap();
assert!(table.set("value", 101).is_err());      // a symbols' value may only be set once

assert_eq!(table.get("value").unwrap(), 42);    // defined symbol
assert!(table.get("undefined").is_err());    // undefined symbol
assert!(table.get("VALUE").is_err());       // undefined because the table is case sensitive

Predefined Symbols

By design of the Hack assembly language we already have predefined symbols inside of our symbol table.

let mut table = SymbolTable::new();

assert!(table.set("R10", 101).is_err());             // built in symbol cannot be redefined
assert_eq!(table.get("SCREEN").unwrap(), 16384);   // predefined
assert_eq!(table.get("R10").unwrap(), 10);         // predefined

Implementations

Sets the value of a symbol to the specified value. This function should also create the symbol if it does not exist. Overwriting a built in or an already defined symbol is not allowed. May return a SymbolTableError

Arguments
  • name - A string that contains the name of the symbol
  • value - The value (or address) associated with the symbol

Retrieves the value of a symbol. May return a SymbolTableError

Arguments
  • name - The symbol name to look up

Trait Implementations

Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.