Skip to content

Commit

Permalink
Finished testing and fine tunning for Assembler! Every basic feature …
Browse files Browse the repository at this point in the history
…(including jump label is working)
  • Loading branch information
LucaDillenburg committed Aug 20, 2020
1 parent 0a1fd5e commit 39fa189
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 16 deletions.
Binary file modified Assembler/assembler
Binary file not shown.
25 changes: 21 additions & 4 deletions Assembler/core/assembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,22 @@ func AssembleFile(path string) {
return
}

errs := ptrProgram.ReplaceLabelsWithNumbers()
if len(errs) > 0 {
for _, err := range errs {
// TODO: create infrastructure go get lineIndex and line here
printErrorOnLine(0, "", *myerrors.NewCodeError(err))
}
printFailedToAssemble(path)
return
}

binaryFileName := helper.FilenameWithoutExtension(file.Name())

binaryFile, err := os.Create(binaryFileName)
if err != nil {
helper.PrintlnErr(fmt.Sprintf("Cannot open file %s . Error: %s", binaryFileName, err.Error()))
printFailedToAssemble(path)
return
}

Expand Down Expand Up @@ -68,9 +79,7 @@ func programFromFile(file io.Reader) *data.Program {
successful = false

for _, err := range errs {
helper.PrintlnErr(fmt.Sprintf("[Error] Error on line %d: '%s'", lineIndex, line))
helper.PrintlnErr(fmt.Sprintf("\t\tError: %s", err.Error()))
helper.PrintlnErr("")
printErrorOnLine(lineIndex, line, err)
}
} else if commandPointer != nil {
program.AddCommand(*commandPointer)
Expand Down Expand Up @@ -139,5 +148,13 @@ func writeBinaryProgram(program data.Program, binaryFileName string, binaryFile
}

func printFailedToAssemble(path string) {
helper.PrintlnErr(fmt.Sprintf("========= Failed to assembly %s =========", path))
helper.PrintlnErr(fmt.Sprintf("========= Failed to assemble %s =========", path))
}

func printErrorOnLine(lineIndex int, line string, err myerrors.CustomError) {
line = utils.RemoveNewLine(line)

helper.PrintlnErr(fmt.Sprintf("[Error] Error on line %d: '%s'", lineIndex, line))
helper.PrintlnErr(fmt.Sprintf("\t\tError: %s", err.Error()))
helper.PrintlnErr("")
}
8 changes: 7 additions & 1 deletion Assembler/core/commandAssembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ func getSecondWord(commandName string, words []string, acceptStringParam bool) (
num, err := utils.StrToPositiveInt(strParam)

if err != nil {
return nil, myerrors.NewCodeError(myerrors.InvalidParamLabelOrInt(strParam, err))
var customMsgErr error
if acceptStringParam {
customMsgErr = myerrors.InvalidParamLabelOrInt(strParam, err)
} else {
customMsgErr = myerrors.InvalidParamInt(strParam, err)
}
return nil, myerrors.NewCodeError(customMsgErr)
}

param := data.NewIntParam(num)
Expand Down
6 changes: 5 additions & 1 deletion Assembler/myerrors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ func InvalidParamLabelOrInt(param string, err error) error {
return fmt.Errorf("Param '%s' is not a valid label nor a valid number (Conversion error: %s)", param, err.Error())
}

func InvalidParamInt(param string, err error) error {
return fmt.Errorf("Param '%s' is not a valid number (Conversion error: %s)", param, err.Error())
}

func InvalidStateTransformationToExecuterError() error {
return errors.New("Invalid State: Cannot transform command to executer while parameter is still a label")
}
Expand All @@ -53,6 +57,6 @@ func CommandDoesNotExistError(commandStr string) error {
return fmt.Errorf("Command '%s' does not exist", commandStr)
}

func InvalidParamError(num int, strLength int, hexStr string) error {
func InvalidNumberParamParseToHexStrError(num int, strLength int, hexStr string) error {
return fmt.Errorf("Number %d cannot be converted to hexadecimal string of length %d. Got: '%s'", num, strLength, hexStr)
}
2 changes: 1 addition & 1 deletion Assembler/utils/conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func IntToStrHex(num int, strLength int) (string, error) {
hexStr := strconv.FormatInt(int64(num), 16)

if len(hexStr) > strLength {
return "", myerrors.InvalidParamError(num, strLength, hexStr)
return "", myerrors.InvalidNumberParamParseToHexStrError(num, strLength, hexStr)
}

for len(hexStr) < strLength {
Expand Down
10 changes: 9 additions & 1 deletion Assembler/utils/normalization.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ import (
)

func LineNormalization(line string) string {
lowerCaseStr := strings.ToLower(line)
withoutNewLine := RemoveNewLine(line)

lowerCaseStr := strings.ToLower(withoutNewLine)
return removeUnecessarySpaces(lowerCaseStr)
}

func RemoveNewLine(line string) string {
lineWithoutEndingUnix := strings.TrimSuffix(line, "\n")
lineWithoutEndingUnixAndWindows := strings.TrimSuffix(lineWithoutEndingUnix, "\r")
return lineWithoutEndingUnixAndWindows
}

func removeUnecessarySpaces(line string) string {
space := regexp.MustCompile(`\s+`)
str := space.ReplaceAllString(line, " ")
Expand Down
34 changes: 27 additions & 7 deletions Assembler/utils/normalization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ func TestLineNormalization(t *testing.T) {
param string
expected string
}{
{"MOV 8", "mov 8"},
{"MoV 8", "mov 8"},
{" Mov 8 ", "mov 8"},
{"Mov acc 8", "mov acc 8"},
{" MOV\t 8 \r\n", "mov 8"},
{"MoV 8\n", "mov 8"},
}

for _, test := range tests {
Expand All @@ -21,14 +19,36 @@ func TestLineNormalization(t *testing.T) {
}
}

func TestRemoveNewLine(t *testing.T) {
var tests = []struct {
param string
expected string
}{
// Unix
{"mov 8\n", "mov 8"},
{"mov 8 \n", "mov 8 "},
// Windows
{"mov acc 8\r\n", "mov acc 8"},
{"mov acc 8 \r\n", "mov acc 8 "},
}

for _, test := range tests {
got := RemoveNewLine(test.param)

if got != test.expected {
t.Errorf("Expected: '%s', Got: '%s'", test.expected, got)
}
}
}

func TestRemoveUnecessarySpaces(t *testing.T) {
var tests = []struct {
param string
expected string
}{
{" mov 8 ", "mov 8"},
{"mov acc 8", "mov acc 8"},
{" mov acc 8", "mov acc 8"},
{"\t mov 8\t ", "mov 8"},
{"mov\tacc \t 8", "mov acc 8"},
{"\tmov \tacc \t 8", "mov acc 8"},
}

for _, test := range tests {
Expand Down
Binary file added Examples/Assembly/assembler
Binary file not shown.
2 changes: 1 addition & 1 deletion Examples/Assembly/assembly
Original file line number Diff line number Diff line change
@@ -1 +1 @@
020c03010401020c0900
020c030104010d00020c0900
4 changes: 4 additions & 0 deletions Examples/Assembly/assembly.asm
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@

jmpHere:

store 12
add 0x1
sub 1

je jmpHere

store 0xc

kill

0 comments on commit 39fa189

Please sign in to comment.