mirror of
https://github.com/dschmenk/PLASMA.git
synced 2025-01-09 13:33:26 +00:00
Merge branch 'master' of https://github.com/dschmenk/PLASMA
This commit is contained in:
commit
ecc010e802
52
README.md
52
README.md
@ -611,7 +611,7 @@ Numbers can be represented in either decimal (base 10), or hexadecimal (base 16)
|
||||
|
||||
## Character and String Literals
|
||||
|
||||
A character literal, represented by a single character or an escaped character enclosed in single quotes `'`, can be used wherever a number is used. String literals, a character sequence enclosed in double quotes `"`, can only appear in a data definition. A length byte will be calculated and prepended to the character data. This is the Pascal style of string definition used throughout PLASMA and ProDOS. When referencing the string, its address is used:
|
||||
A character literal, represented by a single character or an escaped character enclosed in single quotes `'`, can be used wherever a number is used. A length byte will be calculated and prepended to the character data. This is the Pascal style of string definition used throughout PLASMA and ProDOS. When referencing the string, its address is used:
|
||||
|
||||
```
|
||||
char mystring[] = "This is my string; I am very proud of it."
|
||||
@ -640,55 +640,7 @@ Strings can be used as literals inside expression or as parameters. The above pu
|
||||
puts("This is my string; I am very proud of it.")
|
||||
```
|
||||
|
||||
just like any proper language. This makes coding a much simpler task when it comes to spitting out strings to the screen. However (there always has to be a 'However'), nothing comes for free. Since PLASMA doesn't have garbage collection, memory is allocated on the stack frame for the string every time it is encountered. Translation: you can easily chew up many K of memory if you aren't careful. The memory is recovered when the function exits, just like the rest of the local variables.
|
||||
|
||||
Don't do this:
|
||||
|
||||
```
|
||||
word i
|
||||
|
||||
for i = 0 to 10000
|
||||
puts("I am eating all your memory!")
|
||||
next
|
||||
```
|
||||
|
||||
That string will be allocated anew every time through the loop. Instead, you could put the string in initialized memory, create a pointer to it before the loop, or put all the string handling in a function that gets called from inside the loop:
|
||||
|
||||
```
|
||||
byte nicestr = "This is a nice string"
|
||||
word i
|
||||
|
||||
for i = 0 to 10000
|
||||
puts(@nicestr)
|
||||
next
|
||||
```
|
||||
|
||||
or:
|
||||
|
||||
```
|
||||
word i, nicestr
|
||||
|
||||
nicerstr = "This is a nicer string"
|
||||
for i = 0 to 10000
|
||||
puts(nicestr)
|
||||
next
|
||||
```
|
||||
|
||||
or:
|
||||
|
||||
```
|
||||
word i
|
||||
|
||||
def putstr
|
||||
puts("This is a nice string, too")
|
||||
end
|
||||
|
||||
for i = 0 to 10000
|
||||
putstr
|
||||
next
|
||||
```
|
||||
|
||||
If you are curious as to why in-line strings behave this way, it is due to putting the string constant right into the bytecode stream, which makes it easy to compile and interpret. Also, when bytecode is placed in AUX memory (or extended memory in the Apple ///), it relieves the pressure of keeping all the in-line strings in precious main memory all the time. A normal compiler would move in-line strings into anonymous data memory and reference it from there. PLASMA now has a string pool associated with each function invocation, just like the local variable frame. It grows dynamically as strings are encountered and gives them an address in main memory until the function exits, freeing the string pool for that function. PLASMA is too dumb (and I'm too lazy) to implement a real string manager inside the compiler/VM. That would make for a nice library module, though.
|
||||
just like any proper language. This makes coding a much simpler task when it comes to spitting out strings to the screen.
|
||||
|
||||
## Words
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user