Skip to content

Commit

Permalink
Add support for hex numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaScorpion committed Apr 6, 2021
1 parent 3dae55c commit f43b0c4
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 18 deletions.
40 changes: 32 additions & 8 deletions SCRIPTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,38 @@
```
# Lines starting with a "#" are comments.
print "Call functions like this."
sleep 500
print "Done!"
# Store values in variables for easy reusability.
timeToSleep = 500
sleep timeToSleep
print "That's all folks!"
```

## Values

There are 2 kinds of values: strings and integers. Strings are values wrapped in quotes (`"`). Integers are bare numbers, which can be either in decimal or hexadecimal notation.

```
print "I am a string"
vKeyPress 65 # Decimal
vKeyPress 0x41 # Hexadecimal
```

## Variables

Values can be stored in a variable. These variables can be passed to functions just like other values. Variable names can contain letters, numbers, and underscores. Note that they cannot begin with a number.

```
a = 0x41
vKeyPress a
```

## Functions

### `print`

Print zero or more a value to the output. All arguments are concatenated together with a space.
Print zero or more a values to the output. All arguments are concatenated together with a space.

```
print "A simple print."
Expand All @@ -34,7 +57,7 @@ Concatenate multiple 42 values 123
Wait a number of milliseconds before resuming the script.

```
# Sleep for 1 second:
# Sleep for 1 second
sleep 1000
```

Expand All @@ -43,8 +66,9 @@ sleep 1000
Press, down, or up a virtual key. The list of virtual key codes can be found on: https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes

```
vKeyPress 65 # Press a
vKeyDown 16 # Down shift
vKeyPress 65 # Press a
vKeyUp 16 # Up shift
# Type "aA"
vKeyPress 0x41 # Press a
vKeyDown 0x10 # Down shift
vKeyPress 0x41 # Press a
vKeyUp 0x10 # Up shift
```
14 changes: 7 additions & 7 deletions example.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
print "Starting script..."
sleep 10 # milliseconds

msg = "Assignment"
print msg

t = 100
sleep t

# aA
vKeyPress 65
vKeyDown 16
vKeyPress 65
vKeyUp 16
a = 0x41
shift = 0x10
vKeyPress a
vKeyDown shift
vKeyPress a
vKeyUp shift
vKeyPress a
20 changes: 17 additions & 3 deletions internal/lexer/lexFunctions.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ func lexBegin(l *lexer) lexFn {
l.emit(TokenIdentifier)
return lexBegin
case unicode.IsNumber(nextRune):
l.readWhile(unicode.IsNumber)
l.emit(TokenLiteralInt)
return lexBegin
return lexNumberLiteral
case nextRune == quote:
return lexStringLiteral
case nextRune == equals:
Expand Down Expand Up @@ -63,3 +61,19 @@ func lexStringLiteral(l *lexer) lexFn {
l.emit(TokenLiteralString)
return lexBegin
}

func lexNumberLiteral(l *lexer) lexFn {
// Leading number
l.readRune()

if l.peekRune() == hex {
l.readRune()
l.readWhile(unicode.IsNumber)
l.emit(TokenLiteralHex)
} else {
l.readWhile(unicode.IsNumber)
l.emit(TokenLiteralInt)
}

return lexBegin
}
3 changes: 3 additions & 0 deletions internal/lexer/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const (
TokenIdentifier
TokenLiteralString
TokenLiteralInt
TokenLiteralHex
tokenValueEnd

TokenAssign
Expand All @@ -52,6 +53,7 @@ var tokenNames = map[TokenType]string{
TokenIdentifier: "Identifier",
TokenLiteralString: "String",
TokenLiteralInt: "Int",
TokenLiteralHex: "Hex",

TokenAssign: "Assign",
}
Expand All @@ -62,4 +64,5 @@ const (
newline rune = '\n'
quote rune = '"'
equals rune = '='
hex rune = 'x'
)
6 changes: 6 additions & 0 deletions internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ func (p *parser) parseValue() (runtime.Value, error) {
return nil, fmt.Errorf("invalid number value: %s", valueToken.Value)
}
return runtime.NewNumberValue(intVal), nil
case lexer.TokenLiteralHex:
intVal, err := strconv.ParseInt(valueToken.Value, 0, 0)
if err != nil {
return nil, fmt.Errorf("invalid number value: %s", valueToken.Value)
}
return runtime.NewNumberValue(int(intVal)), nil
case lexer.TokenIdentifier:
return runtime.NewVariableValue(valueToken.Value), nil
default:
Expand Down

0 comments on commit f43b0c4

Please sign in to comment.