fixed assignment type error with structs

added structs example
This commit is contained in:
Irmen de Jong 2019-07-16 23:56:00 +02:00
parent 07d8caf884
commit 411bedcc46
5 changed files with 71 additions and 8 deletions

2
.gitignore vendored
View File

@ -24,8 +24,6 @@ __pycache__/
parser.out
parsetab.py
.pytest_cache/
compiler/src/prog8_kotlin.jar
compiler/src/compiled_java
.attach_pid*
.gradle

View File

@ -390,7 +390,8 @@ internal class AstChecker(private val program: Program,
val constVal = assignment.value.constValue(program)
if (constVal != null) {
checkValueTypeAndRange(targetDatatype, constVal)
// TODO what about arrays etc:
// TODO what about arrays, structs, strings etc:
// val targetVar =
// if(target.identifier!=null)
// program.namespace.lookup(target.identifier.nameInSource, assignment) as? VarDecl
@ -1193,7 +1194,7 @@ internal class AstChecker(private val program: Program,
if (number < -32768 || number > 32767)
return err("value '$number' out of range for word")
}
else -> return false
else -> return err("value of type ${value.type} not compatible with $targetDt")
}
return true
}

View File

@ -34,7 +34,6 @@ fun evaluate(expr: IExpression, ctx: EvalContext): RuntimeValue {
return RuntimeValue.fromLv(expr)
}
is ReferenceLiteralValue -> {
TODO("REF $expr")
return RuntimeValue.fromLv(expr, ctx.program.heap)
}
is PrefixExpression -> {

View File

@ -37,8 +37,8 @@ This software is licensed under the GNU GPL 3.0, see https://www.gnu.org/license
:alt: Fully playable tetris clone
Code example
------------
Code examples
-------------
This code calculates prime numbers using the Sieve of Eratosthenes algorithm::
@ -89,13 +89,47 @@ This code calculates prime numbers using the Sieve of Eratosthenes algorithm::
}
when compiled an ran on a C-64 you'll get:
when compiled an ran on a C-64 you get this:
.. image:: _static/primes_example.png
:align: center
:alt: result when run on C-64
The following programs shows a use of the high level ``struct`` type::
%import c64utils
%zeropage basicsafe
~ main {
struct Color {
ubyte red
ubyte green
ubyte blue
}
sub start() {
Color purple = {255, 0, 255}
Color other
other = purple
other.red /= 2
other.green = 10 + other.green / 2
other.blue = 99
c64scr.print_ub(other.red)
c64.CHROUT(',')
c64scr.print_ub(other.green)
c64.CHROUT(',')
c64scr.print_ub(other.blue)
c64.CHROUT('\n')
}
}
when compiled and ran, it prints ``127,10,99`` on the screen.
Design principles and features
------------------------------

31
examples/structs.p8 Normal file
View File

@ -0,0 +1,31 @@
%import c64utils
%zeropage basicsafe
~ main {
struct Color {
ubyte red
ubyte green
ubyte blue
}
sub start() {
Color purple = {255, 0, 255}
Color other
other = purple
other.red /= 2
other.green = 10 + other.green / 2
other.blue = 99
c64scr.print_ub(other.red)
c64.CHROUT(',')
c64scr.print_ub(other.green)
c64.CHROUT(',')
c64scr.print_ub(other.blue)
c64.CHROUT('\n')
}
}