// %PARAMETERS="parse" // Parse YaBASIC source files prior to execution. // lines such as: // gtk_init() will be changed to: // GTK$("gtk_init") // or: // mainwin$ = gtk_window_new (GTK_WINDOW_TOPLEVEL) // will be changed to read: // mainwin$ = GTK$("gtk_window_new GTK_WINDOW_TOPLEVEL") // or // mainwin$ = gtk_window_new(GTK_WINDOW_TOP_LEVEL) // mainwin$=GTK$("gtk_window_new gtk_window_toplevel") // // A new file will be written with the indentifier GTK_ added to the file name. This file will // be the one that is executed. // For simplicity don't have multi-statement GTK_*() lines // The next lines check for any parsing errors. (ConTEXT only) arg$ = peek$("argument") if arg$ = "parse" exit // gosub init while( ! eof(in)) line input #in real_line$ trim_line$ = trim$(real_line$) get_line_elements(trim_line$) // strip comments from statements. Saved in statement$ and comment$ if this_is_a_gtk_line(statement$) then alter_gtk() end if white_space$ = get_tabs$(real_line$) print #out white_space$ + statement$ + comment$ wend add_subs() close in close out print out$ sys$=system$("start c:\yabasic\yabasic.exe \""+out$+"\"") exit end // label init in$ = arg$ ext=instr(in$,".") print in$ pos=rinstr(in$,"\\") out$=left$(in$,pos)+"gtk"+mid$(in$,pos+1,ext-pos)+"yab" print out$ in = open(in$, "r") out = open(out$, "w") return // sub get_line_elements(tmp$) local c_pos statement$ = "" comment$ = "" // '/' = ascii 47 c_pos = instr(tmp$, chr$(47) + chr$(47)) if c_pos = 0 then statement$ = tmp$ else statement$ = left$(tmp$, c_pos - 1) comment$ = mid$(tmp$, c_pos) end if end sub // sub this_is_a_gtk_line(s$) s$ = lower$(s$) if glob(s$, "*g?k_*(*)") or glob(s$,"*g_*(*)") then return true else return false end if end sub // sub get_tabs$(a$) local count local tmp$, w_s$ count = 1 repeat tmp$ = mid$(a$, count, 1) if tmp$ = " " or tmp$ = "\t" then count = count + 1 end if until(tmp$ <> " " and tmp$ <> "\t") w_s$ = left$(a$, count - 1) return w_s$ end sub // sub alter_gtk() local end_pos, num, pos,n,quote local bracket$, gtk_ele$, new_statement$, tmp$ local v$(1) print statement$ pos = instr(lower$(statement$), "gtk_") if pos = 0 pos = instr(lower$(statement$), "gdk_") if pos = 0 pos = instr(lower$(statement$), "g_") new_statement$ = left$(statement$, pos - 1) + "GTK$(" end_pos = instr(statement$, "(", pos) gtk_ele$ = mid$(statement$, pos, end_pos - pos) new_statement$ = new_statement$ + "\"" + gtk_ele$+" " pos = end_pos end_pos = instr(statement$, ")") bracket$ = mid$(statement$,pos+1,end_pos-pos-1) num=token(bracket$,v$(),",") for n=1 to num v$(n)=trim$(v$(n)) // quote=False // if left$(v$(n),1)="'" then quote=True v$(n)=mid$(v$(n),2) v$(n)=left$(v$(n),len(v$(n))-1) end if // if left$(v$(n), 1) = "\"" v$(n) = " \\" + v$(n) // if right$(v$(n), 1) = "\"" then v$(n) = left$(v$(n), len(v$(n)) - 1) + "\\\"" end if // if right$(v$(n), 1) <> "$" then new_statement$ = new_statement$ + v$(n) + " " end if // if right$(v$(n), 1) = "$" and quote = False then // new_statement$ = new_statement$ + "\"" + " + " + v$(n) + " " + "+ \" " new_statement$ = new_statement$ + "\"" + " + " + v$(n) + "+ \" " end if // if right$(v$(n), 1) = "$" and quote = True then new_statement$ = new_statement$ + "\\\"\"" + " + " + v$(n) + " " + "+ \"\\\" " end if // next n // if right$(new_statement$,1)=" " new_statement$=left$(new_statement$,len(new_statement$)-1) // What was I thinking??? new_statement$=rtrim$(new_statement$) new_statement$=new_statement$ + "\")" if right$(new_statement$,6)=" + \"\")" or right$(new_statement$,5)="+\"\")" then new_statement$=left$(new_statement$,len(new_statement$)-5)+")" end if statement$=new_statement$ end sub // sub add_subs() local n,tmp$ repeat read tmp$ if not (glob(tmp$, "Stop*")) then print #out,tmp$ end if until(glob(tmp$, "Stop*")) end sub // data "//////////////////////////////////////" data "// ** Pipe init/closure routines ** //" data "//////////////////////////////////////" data "" data "sub open_pipes()" data "local os$" data "os$ = peek$(\"os\")" data "if (os$ = \"unix\") then" data " system(\"gtk-server fifo=/tmp/yab.pipe &\")" data "sleep 1" data "else" data " system(\"start gtk-server.exe fifo log\") // Logs for debugging only" data " sleep 1" data " open \"\\\\\\\\.\\\\pipe\\\\out\" for writing as 1" data " sleep .5" data " open \"\\\\\\\\.\\\\pipe\\\\in\" for reading as 2" data " sleep .5" data "end if" data "GTK$(\"gtk_init NULL NULL\")" data "end sub" data "" data "sub GTK$(st$)" data "local tp$" data "local os$" data "local streamW, streamR, NewStream" data "os$ = peek$(\"os\")" data "if (os$ = \"unix\") then" data " NewStream=open(\"/tmp/yab.pipe\",\"w\")" data " print #NewStream st$;" data " close NewStream" data " NewStream=open(\"/tmp/yab.pipe\",\"r\")" data " line input #NewStream tp$" data " close NewStream" data " if (glob(st$,\"gtk_exit *\")) then" data " system(\"rm /tmp/yab.pipe\")" data " end if" data "else" data " print #1 st$;" data " line input #2 tp$" data " if (glob(st$,\"gtk_exit *\")) then" data " close 2" data " close 1" data " end if" data "end if" data "return tp$" data "end sub" data "Stop reading data, you fool."