vmtranslator

package
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 25, 2025 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Overview

This package translates VM code to Hack assembly code. The input can be a .vm file or a directory containing .vm files. The output is a .asm file with the same name as the input file or directory.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Tranlate added in v0.3.0

func Tranlate(cw *CodeWriter, vmFile io.Reader) error

func TranslateArithmetic

func TranslateArithmetic(command VMCommand, cnt int) (string, error)

TranslateArithmetic generates the assembly code for VMcommand "add", "sub", "and", "or", "neg", "not", "eq", "gt", or "lt". cnt is a counter for generating unique labels and used for eq, gt, and lt commands.

func TranslateCall

func TranslateCall(functionName string, nArgs int, cnt int) (string, error)

TranslateCall generates the assembly code for VMcommand "call functionName nArgs". functionName is the name of the function, nArgs is the number of arguments, and cnt is a counter for generating unique return labels.

func TranslateFunction

func TranslateFunction(functionName string, nVars int) (string, error)

TranslateFunction generates the assembly code for VMcommand "function functionName nVars". functionName is the name of the function, and nVars is the number of local variables.

func TranslateGoto

func TranslateGoto(label string) (string, error)

TranslateGoto generates the assembly code for VMcommand "goto label". label is the label to jump to.

func TranslateIf

func TranslateIf(label string) (string, error)

TranslateIf generates the assembly code for VMcommand "if-goto label". label is the label to jump to if the top of the stack is not zero.

func TranslateLabel

func TranslateLabel(label string) (string, error)

TranslateLabel generates the assembly code for VMcommand "label label". label is the label name.

func TranslatePushPop

func TranslatePushPop(ctype VMCommandType, seg string, idx int, fileName string) (string, error)

TranslatePushPop generates the assembly code for VMcommand "push segment index" or "pop segment index". fileName is the name of the .vm file. It returns an error if the segment is invalid.

func TranslateReturn

func TranslateReturn() (string, error)

TranslateReturn generates the assembly code for VMcommand "return".

func VMTranslator

func VMTranslator(path string) error

VMTranslator translates VM code to Hack assembly code. The input can be a .vm file or a directory containing .vm files. The output is a .asm file with the same name as the input file or directory.

Types

type CodeScanner

type CodeScanner struct {
	SingleLineCommentPrefix        string // the prefix that indicates a comment. Example: "//"
	MultiLineCommentStart          string
	MultiLineCommentInternalPrefix string
	MultiLineCommentEnd            string
	// contains filtered or unexported fields
}

TODO: integrate this code into the hack assembler project CodeScanner is a struct that reads a file line by line and skips empty lines and comments. It provides a method to get the current line of the scanner without leading and trailing spaces and comments. TODO: support multiple comment prefixes. Example: "//" and "#"

func New

func New(r io.Reader, commentPrefix string) CodeScanner

New creates a new CodeScanner with the given reader and comment prefix

func (CodeScanner) Scan

func (cs CodeScanner) Scan() bool

Scan reads the next line from the scanner and skips empty lines and comments. It returns false if there are no more lines.

func (CodeScanner) Text

func (cs CodeScanner) Text() string

Text returns the current line of the scanner without leading and trailing spaces and comments. It replaces multiple spaces with a single space and removes comments at the end of the line.

type CodeWriter

type CodeWriter struct {
	File         io.Writer
	VmFileStem   string         // the base name of the .vm file without the .vm extension. e.g. "SimpleAdd"
	CommandCount int            // for generating unique labels
	FunctionName string         // for generating unique return labels
	ReturnCount  map[string]int // for generating unique return labels
}

CodeWriter translates VM commands to Hack assembly code and writes the code to an output file.

func NewCodeWriter

func NewCodeWriter(asmFile io.Writer) *CodeWriter

NewCodeWriter creates a new asm file with the given path and returns a CodeWriter. CodeWriter.FileNameStem is set to "", so it must be set before calling WriteCommand.

func NewCodeWriterFromFile added in v0.3.0

func NewCodeWriterFromFile(asmFilePath string) (*CodeWriter, error)

func (*CodeWriter) Write

func (cw *CodeWriter) Write(b []byte) (int, error)

Write writes the given bytes to the output file.

func (*CodeWriter) WriteBootStrap

func (cw *CodeWriter) WriteBootStrap() error

WriteBootStrap writes the bootstrap code to the output file. It initializes the stack pointer and calls Sys.init.

func (*CodeWriter) WriteCommand

func (cw *CodeWriter) WriteCommand(gotoCommand VMCommand) error

WriteCommand writes the assembly code for the given VM command to the output file. It returns an error if the command is invalid. It also updates the internal state of the CodeWriter, which is used for generating unique labels.

func (*CodeWriter) WriteInfinityLoop

func (cw *CodeWriter) WriteInfinityLoop() error

WriteInfinityLoop writes an infinite loop to the output file. It is used to prevent the program from exiting.

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser is a struct that reads VM commands from a file and provides methods to get the command type and arguments

func NewParser

func NewParser(r io.Reader, commentPrefix string) Parser

NewParser creates a new Parser with the given reader and comment prefix. It uses a CodeScanner to read the file. commentPrefix is the prefix that indicates a comment. Example: "//"

type VMCommand

type VMCommand string

VMCommand is a string that represents an instruction. Example: "push constant 7", "add", "label LOOP"

type VMCommandType

type VMCommandType int

VMCommandType is an enum that represents the type of an instruction. C_ARITHMETIC: add, sub, neg, eq, gt, lt, and, or, not C_PUSH: push segment i C_POP: pop segment i C_LABEL: label label C_GOTO: goto label C_IF: if-goto label C_FUNCTION: function function n C_RETURN: return C_CALL: call function n

const (
	C_ARITHMETIC VMCommandType = iota
	C_PUSH
	C_POP
	C_LABEL
	C_GOTO
	C_IF
	C_FUNCTION
	C_RETURN
	C_CALL
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL