'  The most BASIC of spreadsheets
'  By Aaron Addison, rangerfriday@gmail.com
'  verson 0.0.00001


Font 1,1
Option base 1
'CllS
'Print @(0,0) "";

curcell$="A1"

onload
'Sort foru$(),,4

clls

Print "---------"
Pause 500
clls


drawscr
'recalc
'forcerecalc
recalc
'Pause 1200
'menu
'drawscr
'recalc
main

End

Sub showprompt
'Print @(0,MM.FONTWIDTH*3) Space$(MM.WIDTH);
'Print @(0,MM.FONTWIDTH*2) Space$(MM.WIDTH);
'Print @(0,MM.FONTWIDTH*1) Space$(MM.WIDTH);

Print @(0,MM.FONTWIDTH*2) "Command:  (H For help)";
highlightcell
'Print @(0,MM.FONTWIDTH*2) "Command:";


End Sub

Sub highlightcell
'Print @(0,MM.FONTWIDTH*3) Space$(MM.WIDTH);
'Print @(0,MM.FONTWIDTH*2) Space$(MM.WIDTH);
'Print @(0,MM.FONTWIDTH*1) Space$(MM.WIDTH);
disp curcell$
col = Instr(cols$,Mid$(curcell$,1,1))
row = Val(Mid$(curcell$,2))
Print @(MM.FONTWIDTH*6+(MM.FONTWIDTH*(11*(col-1))),MM.FONTHEIGHT*(3+row),2)  Left$(get$("value",curcell$),10) ;
Print @(MM.FONTWIDTH*6+(MM.FONTWIDTH*(11*(col-1))),MM.FONTHEIGHT*(3+row),2)  "         "
Print @(MM.FONTWIDTH*6+(MM.FONTWIDTH*(11*(col-1))),MM.FONTHEIGHT*(3+row),2)  Left$(get$("value",curcell$),10) ;


Print @(0,0) "Cell: "curcell$

Print @(0,MM.FONTWIDTH*2) "Command:";


End Sub


Sub main
showprompt
Do
Pause 50
g$=Inkey$
g$=LCase$(g$)
If g$ = "r" Then forcerecalc
If g$ = "r" Then refresh
If g$ = "l" Then
loaddata
'forcerecalc
'Sort foru$(),,4
'refresh
EndIf
If g$="s" Then savedata
If g$ = "q" Then
  clls
  CLS
  End
EndIf
If g$ = "h" Then
  menu
  refresh
EndIf
If g$="m" Then
  CLS
  clls
  Memory
  Print "Press return to continue ";
  Input prompt$
  refresh
EndIf
If Asc(g$)=131 Then moveto Asc(g$)
If Asc(g$)=130 Then moveto Asc(g$)
If Asc(g$)=129 Then moveto Asc(g$)
If Asc(g$)=128 Then moveto Asc(g$)
If g$="e" Then editcell


Loop


End Sub

Sub editcell
disp curcell$
Print @(0,MM.FONTWIDTH*3) Space$(MM.WIDTH);
Print @(0,MM.FONTWIDTH) Space$(MM.WIDTH);
Print @(0,MM.FONTWIDTH*2) Space$(MM.WIDTH);
Print @(0,0) "Edit Cell "curcell$ ;
Print "-> "
Print "Formulas must start with =, text starts with '"
Print "numbers are entered normally"
Print @(0,0) "Edit Cell "curcell$ ;
Input "-> "; fm$
If fm$ = "" Then Exit Sub
type$="n"
If Mid$(fm$,1,1) = "'" Then type$ ="t"
If Mid$(fm$,1,1) = "=" Then type$ ="f"
If type$ <> "n" Then fm$=Right$(fm$,Len(fm$)-1)
If get$("item",curcell$) = "" Then
maxusedcell=maxusedcell+1
foru$(maxusedcell) = curcell$+":"+type$+":"+fm$+":"+fm$
forcerecalc
Else
'tempadddr = val(get$("item",curcell$))
foru$(Val(get$("item",curcell$))) = curcell$+":"+type$+":"+fm$+":"+fm$
forcerecalc
EndIf
drawscr
recalc

showprompt



End Sub


Sub moveto hitkey
disp curcell$
col = Instr(cols$,Mid$(curcell$,1,1))
row = Val(Mid$(curcell$,2))
If hitkey = 131 Then col = col + 1
If hitkey = 130 Then col = col - 1
If hitkey = 129 Then row = row + 1
If hitkey = 128 Then row = row - 1
If col > 6 Then col = 6
If col < 1 Then col =1
If row > 26 Then row = 26
If row < 1 Then row =1
curcell$ = Mid$(cols$,col,1) + Str$(row)
disp curcell$
'highlightcell
showprompt
End Sub
Print @(0,MM.FONTWIDTH*1) Space$(MM.WIDTH);
Print @(0,MM.FONTWIDTH*2) Space$(MM.WIDTH);
Print @(0,MM.FONTWIDTH*3) Space$(MM.WIDTH);





Sub refresh
changedcounter=0
CLS
clls
drawscr
showprompt
showcells
changedcounter=0
recalc
highlightcell
Print @(0,0) "             Changed cells "changedcounter
Do While changedcounter > 0
changedcounter=0
recalc
Print @(0,0) "             Changed cells "changedcounter
itter=itter+1
If itter = 6 Then changedcounter=0
Loop
highlightcell

End Sub


Sub menu
clls
CLS
Print "Commands:"
Print "H - This Screen"
Print "E - Edit Cell"
Print "S - Save sheet"
Print "A - Save-As Sheet"
Print "L - Load Sheet"
Print "R - ReCalc sheet"
Print "M - Show Memory Used/free"
'Print "P - Edit Preferences"
'Print "! - Reboot"
Print
Print
Print "Arrow Keys will move the cursor and change the selected cell"
'Print "Y G H B Also function as arrow keys"
Print
Print "Q - Quit"
Print
Print "Commands work in both upper and lower case."


Print "Press return to continue ";
Input prompt$

End Sub




Sub forcerecalc
For a = 1 To maxusedcell
popcell$=get$("cell",Str$(a))
force=1
cll$=calc$( popcell$ )
force=0
Next
End Sub


Sub recalc
For a = 1 To maxusedcell
popcell$=get$("cell",Str$(a))
cll$=calc$(popcell$)
Next
End Sub


Sub update(cell$,field$,value$)
Print @(0,0) "UPDATE "cell$
tempupdate=Val(get$("item",cell$))
yield$=cell$+":"+get$("type",cell$)+":"+value$+":"+get$("formula",cell$)
foru$(tempupdate)=yield$
'foru$(1)="A1:t:Cell A1:Cell A1"

'Print "tempupdate"tempupdate
'Print "yeild"yield$
'disp cell$
End Sub



Function calc$(cell$)
Print @(0,0) "R";force;"CALC " cell$;
calc$=cell$
If get$("type",cell$) = "t" Then

If force=1 Then
calc$=get$("formula",cell$)
update(cell$,"value",calc$)
EndIf
disp cell$
EndIf

If get$("type",cell$) = "n" Then
'disp cell$
If force=1 Then
  calc$=get$("formula",cell$)
  update(cell$,"value",calc$)
disp cell$
EndIf
EndIf


If get$("type",cell$) = "f" Then
calc$=get$("formula",cell$)

Do
'Print @(0,10) calc$;
start=Instr(1,calc$,"[A-Z|a-z][0-9]\+",size)
If start=0 Then Exit Do
calctemp$=get$("value",Mid$(calc$,start,size))
If calctemp$ = "" Then calctemp$="0"
MID$(calc$,start,size)=calctemp$'get$("value",Mid$(calc$,start,size))

Loop
oldvalue$=get$("value",cell$)
On error skip
calc$=Left$(Str$(Eval(calc$)),10)
If MM.Errno <> 0 Then calc$="#ERROR"+Str$(MM.Errno)

If calc$ <> oldvalue$ Then changedcounter = changedcounter + 1
update(cell$,"value", calc$)

disp cell$
EndIf
'If force=0 Then disp cell$

Print @(0,MM.FONTWIDTH*1) Space$(MM.WIDTH);


End Function




Function get$(field$,cell$)
Local tempaddr
Local curcell$
Local curtype$
Local curvalue$
Local curformula$
Local curpos

cell$=UCase$(cell$)
For tempaddr = 1 To maxusedcell
'Print foru$(tempaddr)
curcell$=Mid$(foru$(tempaddr),1,Instr(foru$(tempaddr),":")-1)
curpos = Instr(foru$(tempaddr),":")+1
curtype$=Mid$(foru$(tempaddr),curpos,Instr(curpos,foru$(tempaddr),":")-curpos)
'Print "pos "curpos

curpos = Instr(curpos+1,foru$(tempaddr),":")+1
'Print "pos "curpos
curvalue$=Mid$(foru$(tempaddr),curpos,Instr(curpos,foru$(tempaddr),":")-curpos)
curpos = Instr(curpos+1,foru$(tempaddr),":")+1
'Print "pos "curpos
curformula$=Mid$(foru$(tempaddr),curpos)


'Print "current " Curcell$ ""
'Print "type "curtype$
'Print "value "curvalue$
'Print "formula "curformula$


If field$ = "cell" Then
  If Val(cell$) = tempaddr Then get$ = curcell$
  EndIf


If curcell$ = cell$ Then

Select Case field$
 Case "value"
  get$=curvalue$
Case "type"
get$=curtype$
Case "formula"
get$=curformula$
Case "curcell"
get$=curcell$
Case "item"
get$=Str$(tempaddr)
End Select

EndIf

Next



End Function







z$ = "COS(RAD(30)) * 100"

'Print Eval(z$)
End


Sub drawscr
CLS
Local a
Local s
Local counter
'clls
Print @(0,MM.FONTWIDTH*3) Space$(MM.WIDTH);
Print @(0,MM.FONTWIDTH*2) Space$(MM.WIDTH);
Print @(0,MM.FONTWIDTH*1) Space$(MM.WIDTH);


For s= (MM.FONTHEIGHT*4) To MM.FONTHEIGHT*(MM.HEIGHT-4) Step MM.FONTHEIGHT
counter=counter+1
Print @(2+MM.FONTWIDTH,s) Str$(counter);
If counter < 10 Then Print @(2+MM.FONTWIDTH,s) "0" Str$(counter);
Next

For s= (MM.FONTHEIGHT*4) To MM.FONTHEIGHT*(MM.HEIGHT-4) Step MM.FONTHEIGHT
Print @(2,s) "|";
Next

For a = (MM.FONTWIDTH*5) To MM.FONTWIDTH*MM.WIDTH Step MM.FONTWIDTH*11
For s= (MM.FONTHEIGHT*4) To MM.FONTHEIGHT*(MM.HEIGHT-4) Step MM.FONTHEIGHT

Print @(a,s) "|";
Next
Next

For a = (MM.FONTWIDTH*1) To MM.FONTWIDTH*(MM.WIDTH-2) Step MM.FONTWIDTH*1
Print @(a,MM.FONTHEIGHT*3) "_";
Next
counter=0
For a = (MM.FONTWIDTH*7) To MM.FONTWIDTH*(MM.WIDTH-2) Step MM.FONTWIDTH*11
counter = counter + 1
Print @(a,MM.FONTHEIGHT*3) Mid$(cols$,counter,1);
Next

Print @(0,0)"";

Print "A1"

End Sub

Sub showcells
For a = 1 To maxusedcell
popcell$=get$("cell",Str$(a))
disp popcell$
Next
End Sub

Sub disp(cell$)
col = Instr(cols$,Mid$(cell$,1,1))
row = Val(Mid$(cell$,2))

If col > 7 Then Exit Sub

'Print @(MM.FONTWIDTH*7+(MM.FONTWIDTH*(11*(col-1))),MM.FONTHEIGHT*(3+row),2) "         ";
'Pause 20
Print @(MM.FONTWIDTH*6+(MM.FONTWIDTH*(11*(col-1))),MM.FONTHEIGHT*(3+row)) "          |";


Print @(MM.FONTWIDTH*6+(MM.FONTWIDTH*(11*(col-1))),MM.FONTHEIGHT*(3+row))Left$(get$("value",cell$),10);

Print @(0,MM.FONTHEIGHT*2) "Cell Type: ";
Print get$("type",cell$) "   ";
Print "CurVal: "get$("value",cell$)"    ";
If get$("type",cell$) = "f" Then
Print "Formula: =";
Print get$("formula",cell$);
End If
Print "                        ";

End Sub



End

Sub clls
a=0
For s= 0 To MM.FONTHEIGHT*(MM.HEIGHT-3) Step MM.FONTHEIGHT
Print @(a,s,2) Space$(MM.WIDTH);
Pause 10
Print @(a,s) Space$(MM.WIDTH);
Next

Print @(0,0)

End Sub


Sub onload
cols$="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Dim foru$(300) length 100

End Sub

Sub loaddata
Input "Filename: "; file$

Open file$ For input As #1
tempaddr=0
Do
tempaddr = tempaddr +1
Line Input #1, foru$(tempaddr)
maxusedcell=tempaddr
If Eof(#1) = 1 Then Exit Do
Loop
clls
CLS
drawscr
showcells
showprompt
Close #1

End Sub


Sub savedata
Input "Filename: "; file$
Open file$ For output As #1
For tempaddr = 1 To maxusedcell
Print #1, foru$(tempaddr)
Next
Close #1
refresh
End Sub


Sub dump
For a = 1 To maxusedcell
Print a " " foru$(a)
Next
End Sub
