commit f9f92d53b9fd7f4d49a15a860532358e607c4438
parent b118ea33b223c36cde9beffd2fbfa89ad04a5b77
Author: bsandro <email@bsandro.tech>
Date: Thu, 7 Jul 2022 01:23:07 +0300
- Character support for the ORTH opcode
- More extensive assembly code example
Diffstat:
3 files changed, 87 insertions(+), 13 deletions(-)
diff --git a/asm/instruction.go b/asm/instruction.go
@@ -16,7 +16,7 @@ type Instruction struct {
}
func NewInstruction(in string) *Instruction {
- parsingRe := regexp.MustCompile(`[a-zA-Z0-9]+`)
+ parsingRe := regexp.MustCompile(`[a-zA-Z0-9']+`)
input := parsingRe.FindAllString(in, -1)
cnt := len(input)
if cnt == 0 {
@@ -50,17 +50,31 @@ func NewInstruction(in string) *Instruction {
if cnt != 3 {
log.Fatal(errmsg)
}
- val, err := strconv.ParseUint(input[2], 10, 32)
- if err != nil {
- log.Fatal(err)
- }
instr.RegA = NewRegister(input[1])
- instr.Value = uint32(val)
+ instr.Value = parseValue(input[2])
}
return &instr
}
+func parseValue(s string) uint32 {
+ var val uint32
+ if strings.ContainsRune(s, '\'') {
+ s = strings.Trim(s, "'")
+ if len(s) != 1 {
+ log.Fatalf("invalid character value '%s'", s)
+ }
+ val = uint32(s[0])
+ } else {
+ if v, err := strconv.ParseUint(s, 10, 32); err != nil {
+ log.Fatal(err)
+ } else {
+ val = uint32(v)
+ }
+ }
+ return val
+}
+
//@todo make some kind of polymorphism for different opcodes
func (i *Instruction) Pack() uint32 {
const valueMask uint32 = 0x1FFFFFF
diff --git a/asm/instruction_test.go b/asm/instruction_test.go
@@ -27,6 +27,7 @@ func TestInstructionParse(t *testing.T) {
}{
{"cmov 2 3 4", "CMOV 2 3 4 "},
{"orth 1 1020", "ORTH 1 1020"},
+ {"orth 1 'b'", "ORTH 1 98"},
{"outp 6", "OUTP 6 "},
}
diff --git a/sample.ums b/sample.ums
@@ -5,13 +5,72 @@
;arri 1 8 15
;orth 5 493028
-orth 1 97 ; a
-orth 2 98 ; b
-orth 3 99 ; c
-orth 4 10 ; LF
-outp 1
+;orth 1 'b'
+;orth 2 's'
+;orth 3 'a'
+;orth 4 'n'
+;orth 5 10 ; LF
+
+orth 0 64 ; register #0 is 64
+allo 1 0 ; allocate 64 bytes, write allocated array id to #1
+orth 0 0 ; register #0 is 0
+orth 3 1 ; register #3 is 1
+orth 2 'b' ; register #2 is symbol 'b'
+arra 1 0 2 ; array of id from #1 at offset from #0 is set to value from #2 ('s')
+add 0 0 3 ; register #0 is incremented by #3 (that is 1)
+orth 2 's' ;
+arra 1 0 2 ; array second element is set to 's'
+add 0 0 3 ; register #0 += 1
+orth 2 'a' ;
+arra 1 0 2 ;
+add 0 0 3 ;
+orth 2 'n' ;
+arra 1 0 2 ;
+add 0 0 3 ;
+orth 2 'd' ;
+arra 1 0 2 ;
+add 0 0 3 ;
+orth 2 'r' ;
+arra 1 0 2 ;
+add 0 0 3 ;
+orth 2 'o' ;
+arra 1 0 2 ;
+add 0 0 3 ;
+orth 2 10 ; \n symbol
+arra 1 0 2 ; by now the array from #1 is set to string "bsandro"
+add 0 0 3 ; and register #0 is set to that string length with \n included
+
+; now to actually print this
+orth 4 0 ; register #4 is set to 0
+arri 2 1 4 ; read into #2 from array in #1 with offset of #4
+outp 2 ; print value from #2 ('b')
+add 4 4 3 ; array #4 value is incremented by value from #3 (that is 1)
+arri 2 1 4 ;
+outp 2 ; print 's'
+
+add 4 4 3
+arri 2 1 4
+outp 2 ; 'a'
+add 4 4 3
+arri 2 1 4
+outp 2 ; 'n'
+add 4 4 3
+arri 2 1 4
+outp 2 ; 'd'
+add 4 4 3
+arri 2 1 4
+outp 2 ; 'r'
+add 4 4 3
+arri 2 1 4
+outp 2 ; 'o'
+add 4 4 3
+arri 2 1 4
+outp 2 ; '\n'
+
+aban 1 ; free array from #1
+inp 2 ; read symbol from standard input
+outp 2 ; mirror whatever was read
+inp 2
outp 2
-outp 3
-outp 4
halt