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 ¶
- func Tranlate(cw *CodeWriter, vmFile io.Reader) error
- func TranslateArithmetic(command VMCommand, cnt int) (string, error)
- func TranslateCall(functionName string, nArgs int, cnt int) (string, error)
- func TranslateFunction(functionName string, nVars int) (string, error)
- func TranslateGoto(label string) (string, error)
- func TranslateIf(label string) (string, error)
- func TranslateLabel(label string) (string, error)
- func TranslatePushPop(ctype VMCommandType, seg string, idx int, fileName string) (string, error)
- func TranslateReturn() (string, error)
- func VMTranslator(path string) error
- type CodeScanner
- type CodeWriter
- type Parser
- type VMCommand
- type VMCommandType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func TranslateArithmetic ¶
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 ¶
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 ¶
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 ¶
TranslateGoto generates the assembly code for VMcommand "goto label". label is the label to jump to.
func TranslateIf ¶
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 ¶
TranslateLabel generates the assembly code for VMcommand "label label". label is the label name.
func TranslatePushPop ¶
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 ¶
TranslateReturn generates the assembly code for VMcommand "return".
func VMTranslator ¶
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
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 )