////////////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2015 The 8-Bit Bunch. Licensed under the Apache License, Version 1.1 
// (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at <http://www.apache.org/licenses/LICENSE-1.1>.
// Unless required by applicable law or agreed to in writing, software distributed under 
// the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF 
// ANY KIND, either express or implied. See the License for the specific language 
// governing permissions and limitations under the License.
////////////////////////////////////////////////////////////////////////////////////////

//
// Include all imported modules and their data/functions.
//
include "cmdsys.plh"
include "testlib.plh"
//
// Structure definition.
//
struc mystruc
  byte cmd
  word param
  byte[3]
  word data
end
//
// Const expression
//
const constval = 2*(2+3) // a test expression should evaluate to 10
//
// Declare all global variables for this module.
// Note that arrays are declared with prefix []. postfix [], or no [].
// Only arrays with predclared sizes need [ and ], such as "int[3] a".
//
byte[] hello = "Hello, Apple "
byte[] a1 = "1"
byte[] a2 = "]["
byte[] a2p = "][+"
byte[] a2e = "//e"
byte[] a2c = "//c"
byte[] a3 = "///"
byte   constr = "Constant expression = "
byte[] offsets = "Structure offsets:"
word array[] = 1, 10, 100, 1000, 10000
word ptr
byte spaces = "   "
//
// Define functions.
//
def tens(start)
  word i
  i = start
  repeat
    print:hex(i)
    print:str(@spaces)
    print:dec(i)
    print:newln()
    i = i / 10
  until i == 0
end
def ascii
  byte i
  i = 32
  while i < 128
    putc(i)
    i = i + 1
  loop
end
def nums(range)
  word i
  for i = range downto -range step range/10
    puti(i)
    putln
  next
end
export def main(range)
  nums(*range)
  tens(*range*10)
  ascii
  putln
  puts(@hello)
  when MACHID & $C8
    is $08
      puts(@a1)
      break
    is $00
      puts(@a2)
      break
    is $40
      puts(@a2p)
      break
    is $80
      puts(@a2e)
      break
    is $88
      puts(@a2c)
      break
    is $C0
      puts(@a3)
      break
    otherwise
      putc('?')
  wend
  putln
end
ptr = @main
ptr(@array:6)
ptr = @array
puti((ptr):6)
putln
puti(ptr=>6)
putln
puti((ptr).6)
putln
puti(ptr->6)
putln
puts(@offsets)
putln
puti(cmd)
putln
puti(param)
putln
puti(data)
putln
puti(mystruc)
putln
puts(@constr); puti(constval); putln
puts("Hello from in-line string!\n")
puts("Hi \$41pple.\n");
done
