Skip to content

Commit

Permalink
LEN function, FONT statement - loading custom fonts! (see docs)
Browse files Browse the repository at this point in the history
  • Loading branch information
maciej-trebacz committed Apr 16, 2012
1 parent b3bb1a3 commit f111b8d
Show file tree
Hide file tree
Showing 9 changed files with 583 additions and 9 deletions.
28 changes: 26 additions & 2 deletions README.md
Expand Up @@ -9,10 +9,11 @@ Compiler structure and engine is heavily inspired by [Let's Build a Compiler, by
* Boolean operators: & ~ !
* Bit shifts: << >>
* Relational operators: == <> < > <= >=
* Loading custom fonts
* Control structures: IF, LOOP
* Variables (both integer and string)
* Statements: CLS, LOCATE, PRINT, COLOR, KEY, INPUT, POKE, GOTO, RND
* Functions: STR, CHR, VAL, PEEK, SQR
* Statements: CLS, LOCATE, PRINT, COLOR, KEY, INPUT, POKE, GOTO, RND, FONT
* Functions: STR, CHR, VAL, LEN, PEEK, SQR

## How the language looks like

Expand Down Expand Up @@ -177,6 +178,17 @@ Number = VAL(expression)

Returns an integer representation of a number in string variable (e.g. input from user)

### LEN

Usage:

```
DIM Length
Length = LEN(expression)
```

Returns length (how many letters) of a string.

### INPUT

Usage:
Expand Down Expand Up @@ -250,6 +262,18 @@ Result = SQR(expression)

Computes square root of _expression_.

### FONT

Usage:

```
FONT "file.txt"
```

Loads custom font from text file and replaces current terminal font with it. Font file can specify only selected characters.
For a sample font file please refer to _fonts/box.txt_ file. Every letter in font file starts with letter ascii code, and then 8 lines 4 character each follow,
where dot (.) is an empty space and capital o (O) is a solid "pixel".

### END

Program MUST end with an ```END``` statement.
Expand Down
59 changes: 58 additions & 1 deletion asm.go
Expand Up @@ -264,7 +264,7 @@ func Poke() {

func PutChar() {
SkipWhite()
if (Look == '"') {
if Look == '"' {
GetChar()
EmitLine(fmt.Sprintf("SET [0x8000+X], %d", Look))
GetChar()
Expand All @@ -281,6 +281,16 @@ func PutChar() {
EmitLine("AND X, 0x1ff")
}

func Font() {
SkipWhite()
if Look == '"' {
Next()
LoadFont(Value)
Token = '$'
Next()
}
}

func Goto() {
Next()
Token = '$'
Expand All @@ -292,6 +302,53 @@ func Input() {
Call("input")
}

func LoadFont(filename string) {
var error error
var file *os.File
var charCode = 0
var charLine uint8 = 0
var charWord1 uint32 = 0
var charWord2 uint32 = 0

EmitLine(fmt.Sprintf("; loading font: %s", filename))
file, error = os.Open(filename)
if error != nil {
Error(fmt.Sprintf("Couldn't open '%s' font file!", filename))
return
}
reader := bufio.NewReader(file)
for {
line, _, error := reader.ReadLine()
if error != nil {
break
}
if len(line) < 4 {
charCode, _ = strconv.Atoi(string(line))
charWord1 = 0
charWord2 = 0
charLine = 0
} else {
if line[0] == 'O' {
charWord1 = charWord1 | (1 << (charLine + 8))
}
if line[1] == 'O' {
charWord1 = charWord1 | (1 << charLine)
}
if line[2] == 'O' {
charWord2 = charWord2 | (1 << (charLine + 8))
}
if line[3] == 'O' {
charWord2 = charWord2 | (1 << charLine)
}
charLine++
if (charLine == 8) {
EmitLine(fmt.Sprintf("SET [%#x], %#x", 0x8180 + charCode * 2, charWord1))
EmitLine(fmt.Sprintf("SET [%#x], %#x", 0x8181 + charCode * 2, charWord2))
}
}
}
}

func Lib() {
var error error
var file *os.File
Expand Down
Binary file added binaries/font.bin
Binary file not shown.
27 changes: 27 additions & 0 deletions fonts/box.txt
@@ -0,0 +1,27 @@
6
O.O.
O.O.
O.O.
..O.
OOO.
....
....
....
7
....
....
OOO.
..O.
O.O.
O.O.
O.O.
O.O.
15
O.O.
O.O.
O.O.
O.O.
O.O.
O.O.
O.O.
O.O.
6 changes: 6 additions & 0 deletions functions.go
Expand Up @@ -57,3 +57,9 @@ func FuncSqr() {
Call("sqrt")
}

func FuncLen() {
Next()
MatchString("(")
BoolExpression()
Call("strlen")
}
2 changes: 1 addition & 1 deletion lib.dasm
Expand Up @@ -23,7 +23,7 @@
SET B, A
SET A, 0
:strlen1
IFN [B], 0 ; if character is 00 - end of string
IFE [B], 0 ; if character is 00 - end of string
SET PC, POP ; end function
ADD A, 1 ; increment char count
ADD B, 1 ; increment char address
Expand Down

0 comments on commit f111b8d

Please sign in to comment.