Author Topic: Adventures in B  (Read 1891 times)

Tomaaz

  • Guest
Re: Adventures in C
« Reply #15 on: 17. January 2019, 14:45:21 »
If you manage to learn something beyond BASIC it may actually destroy your bad reputation.  ;)

Aurel

  • Regular Member
  • Hero Member
  • *
  • Posts: 605
    • View Profile
Re: Adventures in C
« Reply #16 on: 17. January 2019, 15:09:08 »
Quote
If you manage to learn something beyond BASIC it may actually destroy your bad reputation

my reputation is not bad   8)

As i said before i will continue with C api programming
and testing some lexers/parsers ...this can help me to improve some things,
..and continue to work on my own editor in Oxygen Basic...
my site:BLOG and FORUM
https://aurelsoft.ucoz.com/

Tomaaz

  • Guest
Re: Adventures in C
« Reply #17 on: 18. January 2019, 17:47:25 »
Is a new chapter coming soon?  ;D

Aurel

  • Regular Member
  • Hero Member
  • *
  • Posts: 605
    • View Profile
Re: Adventures in C
« Reply #18 on: 14. February 2019, 23:50:04 »
maybe...

Code: [Select]
'recursive descent token evaluator
#lookahead
int tc=0 : string token
string tokens[5]
tokens[1] = "2"
tokens[2] = "+"
tokens[3] = "3"
tokens[4] = "*"
tokens[5] = "4"

sub gettok(){
tc++
token = tokens[tc]
}
sub expr() as float{
float v = term()
if token = "+": gettok() : v = v + term(): end if
if token = "-": gettok() : v = v - term(): end if
return v
}
sub term() as float{
float v = factor()
if token = "*": gettok() : v = v * factor(): end if
if token = "/": gettok() : v = v / factor(): end if
return v
}
sub factor() as float{
if asc(token)>47  and asc(token)<58
float v = val(token)
end if
gettok()
return v
}

'exec-----------------
gettok() 'start
float res = expr()
print str res

my site:BLOG and FORUM
https://aurelsoft.ucoz.com/

Aurel

  • Regular Member
  • Hero Member
  • *
  • Posts: 605
    • View Profile
Re: Adventures in C
« Reply #19 on: 15. February 2019, 12:27:04 »
and this one with parens

Code: [Select]
'recursive descent token evaluator
#lookahead
int tc=0 : string token
string tokens[7]
tokens[1] = "2"
tokens[2] = "*"
tokens[3] = "("
tokens[4] = "3"
tokens[5] = "+"
tokens[6] = "4"
tokens[7] = ")"

sub gettok()
tc++ : token = tokens[tc]
end sub

sub expr() as float
float v = term()
if token = "+": gettok() : v = v + term(): end if
if token = "-": gettok() : v = v - term(): end if
return v
end sub

sub term() as float
float v = factor()
if token = "*": gettok() : v = v * factor(): end if
if token = "/": gettok() : v = v / factor(): end if
return v
end sub

sub factor() as float
float v
if asc(token)>47  and asc(token)<58 'nums
v = val(token) : gettok()
end if
if asc(token)=40 and asc(token)<>41 'match (...)
gettok() : v = expr() : gettok()
end if
return v
end sub

'exec-----------------
gettok() 'start
float res = expr()
print str res

my site:BLOG and FORUM
https://aurelsoft.ucoz.com/

Aurel

  • Regular Member
  • Hero Member
  • *
  • Posts: 605
    • View Profile
Re: Adventures in B
« Reply #20 on: 16. February 2019, 10:52:18 »
and tiny tokenizer

Code: [Select]
'microB tokenizer
int tkNULL=0,tkPLUS=1,tkMINUS=2,tkMULTI=3,tkDIVIDE=4
int tkCOLON=5,tkCOMMA=6,tkLPAREN=7,tkRPAREN=8,tkLBRACKET=9,tkRBRACKET=10
int tkPRINT=11,tkDOT=12,tkLINE=13,tkCIRCLE=14 ,tkEOL = 20
string tokList[256] : int typList[256]   'token/type arrays
int start , p = 1 ,start = p ,tp ,n      'init
string code,ch,tk ,crlf=chr(13)+chr(10),bf
code = "var1 + 2.5 " ' test or load_src?

sub tokenizer(src as string) as int
'ch = mid(src,p,1) : print "CH:" + ch' get first char
while p <= len(src)       
     ch = mid(src,p,1)                    'get char

 If asc(ch)=32 : p++ : end if             'skip blank space
           
 If (asc(ch)>96 and asc(ch)<123)          ' [a-z]
       print "CH2:" + ch : p--
   while (asc(ch)>96 and asc(ch)<123) or (asc(ch)>47 and asc(ch)<58) ' [a-z0-9]*
       p++:ch = mid(src,p,1) : print "AZ:" + ch
       tk =tk+ch   
   wend
      print "TOK-AZ:" + tk
       tp++ : tokList[tp] = tk : tk="" :p++       
       'return IDENT;
 Elseif (asc(ch)>47 and asc(ch)<58)                    ' [0-9]
p--
    while (asc(ch)>47 and asc(ch)<58) or (asc(ch)=46)  '[0-9[0.0]]*
        p++ : ch = mid(src,p,1):tk = tk + ch
    wend
       tp++ : tokList[tp] = tk : tk="":p++
       'return NUMBER;
 Elseif asc(ch)=43                                     ' [ + ]
       tk = ch : tp++ : tokList[tp] = tk : tk="" :p++  ' set_token
   
 'elseif...
 End if
wend
return tp
end sub

'call tokenizer..tested(ident,numbers)
int tn: tn = tokenizer(code) : print "number of tokens:" + str(tn)
for n = 1 to tn : bf = bf + tokList[n] + crlf : next n
print  bf
« Last Edit: 18. March 2019, 12:48:06 by Aurel »
my site:BLOG and FORUM
https://aurelsoft.ucoz.com/

Tomaaz

  • Guest
Re: Adventures in C
« Reply #21 on: 17. February 2019, 19:33:30 »
"Adventures in C" you say.  Interesting... ???
« Last Edit: 17. February 2019, 19:36:06 by Tomaaz »