Skip to content

Commit

Permalink
Support DB "String".
Browse files Browse the repository at this point in the history
Demonstrate that it works by outputing the length of a string stored
in RAM.
  • Loading branch information
skx committed Jul 1, 2018
1 parent d1e5db6 commit d009c76
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
15 changes: 15 additions & 0 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,21 @@ func (p *Compiler) concatOp() {
// dataOp embeds literal/binary data into the output
func (p *Compiler) dataOp() {
p.nextToken()

// We might have a string, or a series of ints
//
// If it is a string handle that first
if p.curToken.Type == token.STRING {
len := len(p.curToken.Literal)
for i := 0; i < len; i++ {
p.bytecode = append(p.bytecode, byte(p.curToken.Literal[i]))
}
return
}

//
// Otherwise we expect a single int
//
db := p.curToken.Literal
i, _ := strconv.ParseInt(db, 0, 64)
p.bytecode = append(p.bytecode, byte(i))
Expand Down
39 changes: 39 additions & 0 deletions examples/peek-strlen.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#
# This example uses `peek` to read a string character by character
# and output the length of that string.
#
# Unfortunately we can't output the string itself as we have no
# "print character" option.
#
#

#
# #1 -> Address of string
#
# #0 -> Read each byte here
#
# #3 -> Length of string
#

store #3, 0x0000
store #1, string
:loop
peek #0, #1
inc #1
inc #3
cmp #0, 0x00
jmpnz loop

# length will be one too many - so decrease by one
dec #3

store #1, "The length of the string is "
print_str #1
print_int #3
store #1, " bytes\n"
print_str #1
exit

:string
DB "Steve"
DB 0x00

0 comments on commit d009c76

Please sign in to comment.