1
0
mirror of https://github.com/KarolS/millfork.git synced 2024-07-17 11:28:55 +00:00

Allow declaring multiple variables in one line

This commit is contained in:
Karol Stasiak 2019-07-08 19:24:11 +02:00
parent 769f410767
commit 8a5672c2dc
4 changed files with 36 additions and 51 deletions

View File

@ -40,6 +40,12 @@ If not specified, it will be located according to the usual allocation rules.
Only global variables can be initialized that way. Only global variables can be initialized that way.
The behaviour is undefined when targeting a ROM-based platform. The behaviour is undefined when targeting a ROM-based platform.
You can declare multiple variables in one declaration, for example:
byte x, y @$c000, z=6, w
will declare 4 variables: uninitialized variables `x` and `w`, fixed-address variable `y` and initialized variable `z`.
For every variable `x` larger than a byte, extra subvariables are defined: For every variable `x` larger than a byte, extra subvariables are defined:
* if `x` is of type `word` or `pointer`: * if `x` is of type `word` or `pointer`:

View File

@ -105,25 +105,31 @@ abstract class MfParser[T](fileId: String, input: String, currentDirectory: Stri
val globalVariableDefinition: P[Seq[DeclarationStatement]] = variableDefinition(true) val globalVariableDefinition: P[Seq[DeclarationStatement]] = variableDefinition(true)
val localVariableDefinition: P[Seq[DeclarationStatement]] = variableDefinition(false) val localVariableDefinition: P[Seq[DeclarationStatement]] = variableDefinition(false)
def singleVariableDefinition: P[(Position, String, Option[Expression], Option[Expression], Option[MemoryAlignment])] = for {
p <- position()
name <- identifier ~/ HWS ~/ Pass
addr <- ("@" ~/ HWS ~/ mfExpression(1, false)).?.opaque("<address>") ~ HWS
initialValue <- ("=" ~/ HWS ~/ mfExpression(1, false)).? ~ HWS
alignment = None // TODO
} yield (p, name, addr, initialValue, alignment)
def variableDefinition(implicitlyGlobal: Boolean): P[Seq[DeclarationStatement]] = for { def variableDefinition(implicitlyGlobal: Boolean): P[Seq[DeclarationStatement]] = for {
p <- position() p <- position()
bank <- bankDeclaration bank <- bankDeclaration
flags <- variableFlags ~ HWS flags <- variableFlags ~ HWS
typ <- identifier ~ SWS typ <- identifier ~ SWS
name <- identifier ~/ HWS ~/ Pass vars <- singleVariableDefinition.rep(min = 1, sep = "," ~/ HWS)
addr <- ("@" ~/ HWS ~/ mfExpression(1, false)).?.opaque("<address>") ~ HWS
initialValue <- ("=" ~/ HWS ~/ mfExpression(1, false)).? ~ HWS
alignment = None // TODO
_ <- &(EOL) ~/ "" _ <- &(EOL) ~/ ""
} yield { } yield {
Seq(VariableDeclarationStatement(name, typ, vars.map { case (p, name, addr, initialValue, alignment) => VariableDeclarationStatement(name, typ,
bank, bank,
global = implicitlyGlobal || flags("static"), global = implicitlyGlobal || flags("static"),
stack = flags("stack"), stack = flags("stack"),
constant = flags("const"), constant = flags("const"),
volatile = flags("volatile"), volatile = flags("volatile"),
register = flags("register"), register = flags("register"),
initialValue, addr, alignment).pos(p)) initialValue, addr, alignment).pos(p)
}
} }
val paramDefinition: P[ParameterDeclaration] = for { val paramDefinition: P[ParameterDeclaration] = for {

View File

@ -43,8 +43,7 @@ class ByteMathSuite extends FunSuite with Matchers with AppendedClues {
test("Byte addition") { test("Byte addition") {
EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Z80, Cpu.Intel8080, Cpu.Sharp, Cpu.Intel8086)( EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Z80, Cpu.Intel8080, Cpu.Sharp, Cpu.Intel8086)(
""" """
| byte output @$c000 | byte output @$c000, a
| byte a
| void main () { | void main () {
| a = 1 | a = 1
| output = a + a | output = a + a
@ -55,8 +54,7 @@ class ByteMathSuite extends FunSuite with Matchers with AppendedClues {
test("Byte addition 2") { test("Byte addition 2") {
EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Z80, Cpu.Intel8080, Cpu.Sharp, Cpu.Intel8086)( EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Z80, Cpu.Intel8080, Cpu.Sharp, Cpu.Intel8086)(
""" """
| byte output @$c000 | byte output @$c000, a
| byte a
| void main () { | void main () {
| a = 1 | a = 1
| output = a + 65 | output = a + 65
@ -123,10 +121,7 @@ class ByteMathSuite extends FunSuite with Matchers with AppendedClues {
""" """
| array output[3] @$c000 | array output[3] @$c000
| void main () { | void main () {
| byte x | byte x, y, tmpx, tmpy
| byte y
| byte tmpx
| byte tmpy
| tmpx = one() | tmpx = one()
| tmpy = one() | tmpy = one()
| x = tmpx | x = tmpx
@ -201,9 +196,7 @@ class ByteMathSuite extends FunSuite with Matchers with AppendedClues {
EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Z80, Cpu.Intel8080, Cpu.Sharp)( EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Z80, Cpu.Intel8080, Cpu.Sharp)(
""" """
| import zp_reg | import zp_reg
| byte output1 @$c001 | byte output1 @$c001, output2 @$c002, output3 @$c003
| byte output2 @$c002
| byte output3 @$c003
| void main () { | void main () {
| calc1() | calc1()
| crash_if_bad() | crash_if_bad()
@ -309,10 +302,7 @@ class ByteMathSuite extends FunSuite with Matchers with AppendedClues {
EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Z80, Cpu.Intel8080, Cpu.Sharp, Cpu.Intel8086)( EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Z80, Cpu.Intel8080, Cpu.Sharp, Cpu.Intel8086)(
s""" s"""
| import zp_reg | import zp_reg
| byte output_q1 @$$c000 | byte output_q1 @$$c000, output_m1 @$$c001, output_q2 @$$c002, output_m2 @$$c003
| byte output_m1 @$$c001
| byte output_q2 @$$c002
| byte output_m2 @$$c003
| void main () { | void main () {
| byte a | byte a
| a = f() | a = f()
@ -351,10 +341,8 @@ class ByteMathSuite extends FunSuite with Matchers with AppendedClues {
EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Z80, Cpu.Intel8080, Cpu.Sharp, Cpu.Intel8086)( EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Z80, Cpu.Intel8080, Cpu.Sharp, Cpu.Intel8086)(
s""" s"""
| import zp_reg | import zp_reg
| byte output_q1 @$$c000 | byte output_q1 @$$c000, output_m1 @$$c001
| byte output_m1 @$$c001 | byte output_q2 @$$c002, output_m2 @$$c003
| byte output_q2 @$$c002
| byte output_m2 @$$c003
| void main () { | void main () {
| byte a | byte a
| byte b | byte b
@ -412,10 +400,8 @@ class ByteMathSuite extends FunSuite with Matchers with AppendedClues {
EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Z80, Cpu.Intel8080, Cpu.Sharp, Cpu.Intel8086)( EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Z80, Cpu.Intel8080, Cpu.Sharp, Cpu.Intel8086)(
s""" s"""
| import zp_reg | import zp_reg
| byte output_q1 @$$c000 | byte output_q1 @$$c000, output_m1 @$$c001
| byte output_m1 @$$c001 | byte output_q2 @$$c002, output_m2 @$$c003
| byte output_q2 @$$c002
| byte output_m2 @$$c003
| void main () { | void main () {
| byte a | byte a
| output_q2 = g() | output_q2 = g()

View File

@ -13,8 +13,7 @@ class StackVarSuite extends FunSuite with Matchers {
EmuCrossPlatformBenchmarkRun(Cpu.StrictMos, Cpu.Z80, Cpu.Intel8080, Cpu.Intel8085, Cpu.Sharp, Cpu.Intel8086)(""" EmuCrossPlatformBenchmarkRun(Cpu.StrictMos, Cpu.Z80, Cpu.Intel8080, Cpu.Intel8085, Cpu.Sharp, Cpu.Intel8086)("""
| byte output @$c000 | byte output @$c000
| void main () { | void main () {
| stack byte a | stack byte a, b
| stack byte b
| b = 4 | b = 4
| a = b | a = b
| output = a | output = a
@ -27,8 +26,7 @@ class StackVarSuite extends FunSuite with Matchers {
EmuCrossPlatformBenchmarkRun(Cpu.StrictMos, Cpu.Z80, Cpu.Intel8080, Cpu.Sharp, Cpu.Intel8086)(""" EmuCrossPlatformBenchmarkRun(Cpu.StrictMos, Cpu.Z80, Cpu.Intel8080, Cpu.Sharp, Cpu.Intel8086)("""
| byte output @$c000 | byte output @$c000
| void main () { | void main () {
| stack byte a | stack byte a, b
| stack byte b
| a = $11 | a = $11
| b = $44 | b = $44
| b += zzz() | b += zzz()
@ -74,8 +72,7 @@ class StackVarSuite extends FunSuite with Matchers {
""" """
| byte output @$c000 | byte output @$c000
| void main () { | void main () {
| stack byte a | stack byte a, b
| stack byte b
| b = $77 | b = $77
| a = $11 | a = $11
| b -= zzz() | b -= zzz()
@ -94,8 +91,7 @@ class StackVarSuite extends FunSuite with Matchers {
EmuCrossPlatformBenchmarkRun(Cpu.StrictMos, Cpu.Z80, Cpu.Intel8080, Cpu.Intel8085, Cpu.Sharp, Cpu.Intel8086)(""" EmuCrossPlatformBenchmarkRun(Cpu.StrictMos, Cpu.Z80, Cpu.Intel8080, Cpu.Intel8085, Cpu.Sharp, Cpu.Intel8086)("""
| word output @$c000 | word output @$c000
| void main () { | void main () {
| stack word a | stack word a, b
| stack word b
| a = $111 | a = $111
| b = $444 | b = $444
| b += zzz() | b += zzz()
@ -123,9 +119,7 @@ class StackVarSuite extends FunSuite with Matchers {
| } | }
| word fib(byte i) { | word fib(byte i) {
| stack byte j | stack byte j
| stack word sum | stack word sum, w, v
| stack word w
| stack word v
| j = i | j = i
| if j < 2 { | if j < 2 {
| return 1 | return 1
@ -208,8 +202,7 @@ class StackVarSuite extends FunSuite with Matchers {
EmuCrossPlatformBenchmarkRun(Cpu.StrictMos, Cpu.Z80, Cpu.Intel8080, Cpu.Sharp, Cpu.Intel8086)(""" EmuCrossPlatformBenchmarkRun(Cpu.StrictMos, Cpu.Z80, Cpu.Intel8080, Cpu.Sharp, Cpu.Intel8086)("""
| array output [200] @$c000 | array output [200] @$c000
| void main () { | void main () {
| stack byte a | stack byte a, b
| stack byte b
| a = $11 | a = $11
| b = $44 | b = $44
| output[a + b] = $66 | output[a + b] = $66
@ -286,8 +279,7 @@ class StackVarSuite extends FunSuite with Matchers {
""" """
| int32 output @$c000 | int32 output @$c000
| void main () { | void main () {
| stack int32 a | stack int32 a, b
| stack int32 b
| a = f() | a = f()
| barrier() | barrier()
| b = a | b = a
@ -406,9 +398,7 @@ class StackVarSuite extends FunSuite with Matchers {
| noinline byte f() = 6 | noinline byte f() = 6
| noinline void g(byte x) {} | noinline void g(byte x) {}
| void main () { | void main () {
| stack byte b | stack byte b, a, c
| stack byte a
| stack byte c
| b = b | b = b
| c = c | c = c
| a = f() | a = f()
@ -435,10 +425,7 @@ class StackVarSuite extends FunSuite with Matchers {
| p[0] = __loop.addr + 1 | p[0] = __loop.addr + 1
| } | }
| void main () { | void main () {
| stack word b | stack word b, a, c, d
| stack word a
| stack word c
| stack word d
| h(a.pointer) | h(a.pointer)
| h(b.pointer) | h(b.pointer)
| h(c.pointer) | h(c.pointer)