mirror of
https://github.com/irmen/prog8.git
synced 2025-01-11 13:29:45 +00:00
simplified module grammar rules
This commit is contained in:
parent
1382728bd2
commit
2b28493bba
@ -22,7 +22,9 @@ private data class NumericLiteral(val number: Number, val datatype: DataType)
|
||||
|
||||
fun prog8Parser.ModuleContext.toAst(name: String, isLibrary: Boolean, source: Path) : Module {
|
||||
val nameWithoutSuffix = if(name.endsWith(".p8")) name.substringBeforeLast('.') else name
|
||||
return Module(nameWithoutSuffix, modulestatement().asSequence().map { it.toAst(isLibrary) }.toMutableList(), toPosition(), isLibrary, source)
|
||||
val directives = this.directive().map { it.toAst() }
|
||||
val blocks = this.block().map { it.toAst(isLibrary) }
|
||||
return Module(nameWithoutSuffix, (directives + blocks).toMutableList(), toPosition(), isLibrary, source)
|
||||
}
|
||||
|
||||
|
||||
@ -39,17 +41,6 @@ private fun ParserRuleContext.toPosition() : Position {
|
||||
}
|
||||
|
||||
|
||||
private fun prog8Parser.ModulestatementContext.toAst(isInLibrary: Boolean) : Statement {
|
||||
val directive = directive()?.toAst()
|
||||
if(directive!=null) return directive
|
||||
|
||||
val block = block()?.toAst(isInLibrary)
|
||||
if(block!=null) return block
|
||||
|
||||
throw FatalAstException(text)
|
||||
}
|
||||
|
||||
|
||||
private fun prog8Parser.BlockContext.toAst(isInLibrary: Boolean) : Statement =
|
||||
Block(identifier().text, integerliteral()?.toAst()?.number?.toInt(), statement_block().toAst(), isInLibrary, toPosition())
|
||||
|
||||
@ -59,87 +50,95 @@ private fun prog8Parser.Statement_blockContext.toAst(): MutableList<Statement> =
|
||||
|
||||
|
||||
private fun prog8Parser.StatementContext.toAst() : Statement {
|
||||
vardecl()?.let { return it.toAst() }
|
||||
variabledeclaration()?.let { variable ->
|
||||
variable.vardecl()?.let { return it.toAst() }
|
||||
|
||||
varinitializer()?.let {
|
||||
val vd = it.vardecl()
|
||||
return VarDecl(
|
||||
VarDeclType.VAR,
|
||||
vd.datatype()?.toAst() ?: DataType.STRUCT,
|
||||
if(vd.ZEROPAGE() != null) ZeropageWish.PREFER_ZEROPAGE else ZeropageWish.DONTCARE,
|
||||
vd.arrayindex()?.toAst(),
|
||||
vd.varname.text,
|
||||
null,
|
||||
it.expression().toAst(),
|
||||
vd.ARRAYSIG() != null || vd.arrayindex() != null,
|
||||
false,
|
||||
it.toPosition()
|
||||
)
|
||||
}
|
||||
variable.varinitializer()?.let {
|
||||
val vd = it.vardecl()
|
||||
return VarDecl(
|
||||
VarDeclType.VAR,
|
||||
vd.datatype()?.toAst() ?: DataType.STRUCT,
|
||||
if (vd.ZEROPAGE() != null) ZeropageWish.PREFER_ZEROPAGE else ZeropageWish.DONTCARE,
|
||||
vd.arrayindex()?.toAst(),
|
||||
vd.varname.text,
|
||||
null,
|
||||
it.expression().toAst(),
|
||||
vd.ARRAYSIG() != null || vd.arrayindex() != null,
|
||||
false,
|
||||
it.toPosition()
|
||||
)
|
||||
}
|
||||
|
||||
structvarinitializer()?.let {
|
||||
val vd = it.structvardecl()
|
||||
return VarDecl(
|
||||
VarDeclType.VAR,
|
||||
DataType.STRUCT,
|
||||
ZeropageWish.NOT_IN_ZEROPAGE,
|
||||
null,
|
||||
vd.varname.text,
|
||||
vd.structname.text,
|
||||
it.expression().toAst(),
|
||||
isArray = false,
|
||||
autogeneratedDontRemove = false,
|
||||
position = it.toPosition()
|
||||
)
|
||||
}
|
||||
variable.structvarinitializer()?.let {
|
||||
val vd = it.structvardecl()
|
||||
return VarDecl(
|
||||
VarDeclType.VAR,
|
||||
DataType.STRUCT,
|
||||
ZeropageWish.NOT_IN_ZEROPAGE,
|
||||
null,
|
||||
vd.varname.text,
|
||||
vd.structname.text,
|
||||
it.expression().toAst(),
|
||||
isArray = false,
|
||||
autogeneratedDontRemove = false,
|
||||
position = it.toPosition()
|
||||
)
|
||||
}
|
||||
|
||||
structvardecl()?.let {
|
||||
return VarDecl(
|
||||
VarDeclType.VAR,
|
||||
DataType.STRUCT,
|
||||
ZeropageWish.NOT_IN_ZEROPAGE,
|
||||
null,
|
||||
it.varname.text,
|
||||
it.structname.text,
|
||||
null,
|
||||
isArray = false,
|
||||
autogeneratedDontRemove = false,
|
||||
position = it.toPosition()
|
||||
)
|
||||
}
|
||||
variable.structvardecl()?.let {
|
||||
return VarDecl(
|
||||
VarDeclType.VAR,
|
||||
DataType.STRUCT,
|
||||
ZeropageWish.NOT_IN_ZEROPAGE,
|
||||
null,
|
||||
it.varname.text,
|
||||
it.structname.text,
|
||||
null,
|
||||
isArray = false,
|
||||
autogeneratedDontRemove = false,
|
||||
position = it.toPosition()
|
||||
)
|
||||
}
|
||||
|
||||
constdecl()?.let {
|
||||
val cvarinit = it.varinitializer()
|
||||
val vd = cvarinit.vardecl()
|
||||
return VarDecl(
|
||||
VarDeclType.CONST,
|
||||
vd.datatype()?.toAst() ?: DataType.STRUCT,
|
||||
if(vd.ZEROPAGE() != null) ZeropageWish.PREFER_ZEROPAGE else ZeropageWish.DONTCARE,
|
||||
vd.arrayindex()?.toAst(),
|
||||
vd.varname.text,
|
||||
null,
|
||||
cvarinit.expression().toAst(),
|
||||
vd.ARRAYSIG() != null || vd.arrayindex() != null,
|
||||
false,
|
||||
cvarinit.toPosition()
|
||||
)
|
||||
}
|
||||
variable.constdecl()?.let {
|
||||
val cvarinit = it.varinitializer()
|
||||
val vd = cvarinit.vardecl()
|
||||
return VarDecl(
|
||||
VarDeclType.CONST,
|
||||
vd.datatype()?.toAst() ?: DataType.STRUCT,
|
||||
if (vd.ZEROPAGE() != null) ZeropageWish.PREFER_ZEROPAGE else ZeropageWish.DONTCARE,
|
||||
vd.arrayindex()?.toAst(),
|
||||
vd.varname.text,
|
||||
null,
|
||||
cvarinit.expression().toAst(),
|
||||
vd.ARRAYSIG() != null || vd.arrayindex() != null,
|
||||
false,
|
||||
cvarinit.toPosition()
|
||||
)
|
||||
}
|
||||
|
||||
memoryvardecl()?.let {
|
||||
val mvarinit = it.varinitializer()
|
||||
val vd = mvarinit.vardecl()
|
||||
return VarDecl(
|
||||
VarDeclType.MEMORY,
|
||||
vd.datatype()?.toAst() ?: DataType.STRUCT,
|
||||
if(vd.ZEROPAGE() != null) ZeropageWish.PREFER_ZEROPAGE else ZeropageWish.DONTCARE,
|
||||
vd.arrayindex()?.toAst(),
|
||||
vd.varname.text,
|
||||
null,
|
||||
mvarinit.expression().toAst(),
|
||||
vd.ARRAYSIG() != null || vd.arrayindex() != null,
|
||||
false,
|
||||
mvarinit.toPosition()
|
||||
)
|
||||
variable.memoryvardecl()?.let {
|
||||
val mvarinit = it.varinitializer()
|
||||
val vd = mvarinit.vardecl()
|
||||
return VarDecl(
|
||||
VarDeclType.MEMORY,
|
||||
vd.datatype()?.toAst() ?: DataType.STRUCT,
|
||||
if (vd.ZEROPAGE() != null) ZeropageWish.PREFER_ZEROPAGE else ZeropageWish.DONTCARE,
|
||||
vd.arrayindex()?.toAst(),
|
||||
vd.varname.text,
|
||||
null,
|
||||
mvarinit.expression().toAst(),
|
||||
vd.ARRAYSIG() != null || vd.arrayindex() != null,
|
||||
false,
|
||||
mvarinit.toPosition()
|
||||
)
|
||||
}
|
||||
|
||||
variable.structdecl()?.let {
|
||||
return StructDecl(it.identifier().text,
|
||||
it.vardecl().map { vd->vd.toAst() }.toMutableList(),
|
||||
toPosition())
|
||||
}
|
||||
}
|
||||
|
||||
assignment()?.let {
|
||||
@ -175,8 +174,16 @@ private fun prog8Parser.StatementContext.toAst() : Statement {
|
||||
val returnstmt = returnstmt()?.toAst()
|
||||
if(returnstmt!=null) return returnstmt
|
||||
|
||||
val sub = subroutine()?.toAst()
|
||||
if(sub!=null) return sub
|
||||
subroutinedeclaration()?.let {
|
||||
if(it.subroutine()!=null)
|
||||
return it.subroutine().toAst()
|
||||
|
||||
if(it.asmsubroutine()!=null)
|
||||
return it.asmsubroutine().toAst()
|
||||
|
||||
if(it.romsubroutine()!=null)
|
||||
return it.romsubroutine().toAst()
|
||||
}
|
||||
|
||||
val asm = inlineasm()?.toAst()
|
||||
if(asm!=null) return asm
|
||||
@ -199,21 +206,9 @@ private fun prog8Parser.StatementContext.toAst() : Statement {
|
||||
val continuestmt = continuestmt()?.toAst()
|
||||
if(continuestmt!=null) return continuestmt
|
||||
|
||||
val asmsubstmt = asmsubroutine()?.toAst()
|
||||
if(asmsubstmt!=null) return asmsubstmt
|
||||
|
||||
val romsubstmt = romsubroutine()?.toAst()
|
||||
if(romsubstmt!=null) return romsubstmt
|
||||
|
||||
val whenstmt = whenstmt()?.toAst()
|
||||
if(whenstmt!=null) return whenstmt
|
||||
|
||||
structdecl()?.let {
|
||||
return StructDecl(it.identifier().text,
|
||||
it.vardecl().map { vd->vd.toAst() }.toMutableList(),
|
||||
toPosition())
|
||||
}
|
||||
|
||||
throw FatalAstException("unprocessed source text (are we missing ast conversion rules for parser elements?): $text")
|
||||
}
|
||||
|
||||
|
@ -66,21 +66,13 @@ ARRAYSIG :
|
||||
;
|
||||
|
||||
|
||||
module : (modulestatement | EOL)* EOF ;
|
||||
|
||||
modulestatement: directive | block ;
|
||||
module : (directive | block | EOL)* EOF ;
|
||||
|
||||
block: identifier integerliteral? statement_block EOL ;
|
||||
|
||||
statement :
|
||||
directive
|
||||
| varinitializer
|
||||
| structvarinitializer
|
||||
| vardecl
|
||||
| structvardecl
|
||||
| constdecl
|
||||
| memoryvardecl
|
||||
| structdecl
|
||||
| variabledeclaration
|
||||
| assignment
|
||||
| augassignment
|
||||
| unconditionaljump
|
||||
@ -88,9 +80,7 @@ statement :
|
||||
| functioncall_stmt
|
||||
| if_stmt
|
||||
| branch_stmt
|
||||
| subroutine
|
||||
| asmsubroutine
|
||||
| romsubroutine
|
||||
| subroutinedeclaration
|
||||
| inlineasm
|
||||
| returnstmt
|
||||
| forloop
|
||||
@ -103,6 +93,24 @@ statement :
|
||||
;
|
||||
|
||||
|
||||
variabledeclaration :
|
||||
varinitializer
|
||||
| structvarinitializer
|
||||
| vardecl
|
||||
| structvardecl
|
||||
| constdecl
|
||||
| memoryvardecl
|
||||
| structdecl
|
||||
;
|
||||
|
||||
|
||||
subroutinedeclaration :
|
||||
subroutine
|
||||
| asmsubroutine
|
||||
| romsubroutine
|
||||
;
|
||||
|
||||
|
||||
labeldef : identifier ':' ;
|
||||
|
||||
unconditionaljump : 'goto' (integerliteral | scoped_identifier) ;
|
||||
|
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user