Author Topic: Adventures in B  (Read 5908 times)

Tomaaz

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

Aurel

  • Guest
Re: Adventures in C
« Reply #16 on: January 17, 2019, 02:09:08 PM »
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...

Tomaaz

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

Aurel

  • Guest
Re: Adventures in C
« Reply #18 on: February 14, 2019, 10:50:04 PM »
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


Aurel

  • Guest
Re: Adventures in C
« Reply #19 on: February 15, 2019, 11:27:04 AM »
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


Aurel

  • Guest
Re: Adventures in B
« Reply #20 on: February 16, 2019, 09:52:18 AM »
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: March 18, 2019, 11:48:06 AM by Aurel »

Tomaaz

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