Struct hack_asm::SymbolTable
source · [−]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
| Symbol | Value |
|---|---|
| R0 | 0 |
| R1 | 1 |
| … | … |
| R15 | 15 |
Input/Output
| Symbol | Value |
|---|---|
| SCREEN | 16384 |
| KBD | 24576 |
Reserved
| Symbol | Value |
|---|---|
| SP | 0 |
| LCL | 1 |
| ARG | 2 |
| THIS | 3 |
| That | 4 |
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 sensitivePredefined 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); // predefinedImplementations
sourceimpl SymbolTable
impl SymbolTable
pub fn new() -> Self
sourcepub fn set(
&mut self,
name: &str,
value: HackInt
) -> Result<(), SymbolTableError>
pub fn set(
&mut self,
name: &str,
value: HackInt
) -> Result<(), SymbolTableError>
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 symbolvalue- The value (or address) associated with the symbol
sourcepub fn get(&self, name: &str) -> Result<HackInt, SymbolTableError>
pub fn get(&self, name: &str) -> Result<HackInt, SymbolTableError>
Retrieves the value of a symbol.
May return a SymbolTableError
Arguments
name- The symbol name to look up
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for SymbolTable
impl Send for SymbolTable
impl Sync for SymbolTable
impl Unpin for SymbolTable
impl UnwindSafe for SymbolTable
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more