Computer Science Canada

use of include statement with variable filename

Author:  SuperGenius [ Sun May 16, 2004 6:04 pm ]
Post subject:  use of include statement with variable filename

This is part of the code from my drawing program and what I'm trying to do is get the program to write data to a .t file to take some of the sting out of complicated drawings. I want the program to include the file that it is writing to so that the user can develop a drawing using the stuff they've already done as a base so that the drawing wont look like a blind person did it. I set it up so that the user inputs a word and the program tacks .t onto the end of the string but I get an error "illegal include statement"

code:

put "enter output file name *no .t"
get outfile
outfile := outfile + ".t"

open : input, outfile, get
loop
    get : input, lines (count)
    exit when eof (input)
    count := count + 1
    new lines, count
end loop

close : input
open : input, outfile, put
put : input, "%Drawer output"
close : input

put "enter x size"
get screenx
put "enter y size"
get screeny
setscr := "Graphics: " + intstr (screenx) + ";" + intstr (screeny)
setscreen (setscr)
open : output, outfile, put
fork exitc
for a : 1 .. count
    put : output, lines (count)
end for
loop

    comtype := 0
    exitcheck := false
    exitin := ""
    put "enter command type"
    put "1 for Draw.Line"
    put "2 for Draw.Box or Draw.FillBox"
    get comtype
    put "enter colour number"
    get col
    if comtype = 1 then
        line
    elsif comtype = 2 then
        box
    end if
    include outfile
    loop
        exit when exitcheck = true
    end loop
end loop

Author:  Tony [ Sun May 16, 2004 6:11 pm ]
Post subject: 

you can't do that. include file must be static at compile time since the program does not contain the compiler to execute variable commands at runtime.

If that is what you really want to do though, you can write an enterpriter to recognize the commands in the file and execute them.

Author:  SuperGenius [ Mon May 17, 2004 3:53 pm ]
Post subject: 

tony wrote:


If that is what you really want to do though, you can write an enterpriter to recognize the commands in the file and execute them.


two questions:

How would I do this?

Would it be difficult?

Author:  Tony [ Mon May 17, 2004 6:44 pm ]
Post subject: 

its not neccessary difficult. I've made an enterpriter in turing before.

basically you assume perfect syntax and read one command at a time. Then according to what it is, read the number of parameters required. Such as:
code:

get : fileStream, command

if command = "Draw.Box") then
     get : fileStream, x1
     get : fileStream, y1
     get : fileStream, x2
     get : fileStream, y2

Draw.Box(x1,y1,x2,y2)
end if


obviously you'd have to cut out all the extra junk such as ()s and ,s but you could (and should) use them as your flags in conjunction with index() to cut the code line into command and arguments.


: