Author Topic: Reading and parsing a string  (Read 4625 times)

0 Members and 3 Guests are viewing this topic.

Offline aeTIos

  • Nonbinary computing specialist
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3915
  • Rating: +184/-32
    • View Profile
    • wank.party
Reading and parsing a string
« on: April 14, 2011, 12:20:11 pm »
Hello, I am experimenting with a possible 83+ Lua interpreter, and I wondered how I can parse strings best. Should I use tokens or just input?
If input, how can I parse it best (via an interpreter, it reads a program)
If tokens, can I change token names in some way?

Thanks!
I'm not a nerd but I pretend:

Ashbad

  • Guest
Re: Reading and parsing a string
« Reply #1 on: April 14, 2011, 01:41:18 pm »
well, in assembly it might be much smaller, but here is an axe code for doing that:

Code: [Select]
Lbl PAR
"PARSE :)" -> Str1
"PARTY :D" -> Str2
Length(Str1) -> B
For(A,0,B
!If {Str1+A} = {Str2+A}
-1 -> A  (this is a -1, not a negative 1)
End
If A = B
  //do stuff that needs to be done if it parsed the same
End

This takes Str1 as the example and Str2 as the thing compared to the example.  It would be pretty fast  - though like I said in assembly would be much faster :)  Also, the string being parsed cannot be 65535 characters long, so keep that in mind :P

Offline aeTIos

  • Nonbinary computing specialist
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3915
  • Rating: +184/-32
    • View Profile
    • wank.party
Re: Reading and parsing a string
« Reply #2 on: April 14, 2011, 01:43:07 pm »
Haha lol it won't be that long I think :)
Thanks!
I'm not a nerd but I pretend:

Ashbad

  • Guest
Re: Reading and parsing a string
« Reply #3 on: April 14, 2011, 01:44:13 pm »
Maybe I can even give you an assembly example, in just a minute ;)  Just a second, I'll edit it in.

Code: [Select]
  LD HL, Example
  LD DE, StringToBeParsed
  LD B, LengthOfExample
Loop
  EX DE,HL
  LD A,(HL)
  EX DE,HL
  CPD
  JR NZ,NotSame
  DJNZ Loop
Same
  ;code for what to do here
NotSame
  RET

That should be a more optimized version, in assembly ;)

Hope you likes, since I am not an asm master, it could be optimized more I'm sure, but this alone is much better than the axe version :)
 
« Last Edit: April 14, 2011, 02:02:05 pm by Ashbad »

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: Reading and parsing a string
« Reply #4 on: April 14, 2011, 01:44:34 pm »
Haha lol it won't be that long I think :)
Thanks!

Never assume that when it comes to users :P
« Last Edit: April 14, 2011, 01:46:34 pm by Qwerty.55 »
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline aeTIos

  • Nonbinary computing specialist
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3915
  • Rating: +184/-32
    • View Profile
    • wank.party
Re: Reading and parsing a string
« Reply #5 on: April 14, 2011, 01:45:37 pm »
Never assume that when it comes to users :P
You mean, strings >65535 bytes? Lol.
I'm not a nerd but I pretend:

Ashbad

  • Guest
Re: Reading and parsing a string
« Reply #6 on: April 14, 2011, 01:55:16 pm »
I edited into my second post the assembly version ;)  It should be much smaller and faster.  Though, you might wanna disassemble it to get the Hex code :( or I can try right now in IRC :)  I'll edit in the hex in a moment

EDIT: or not ;)
« Last Edit: April 14, 2011, 01:57:17 pm by Ashbad »

Offline aeTIos

  • Nonbinary computing specialist
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3915
  • Rating: +184/-32
    • View Profile
    • wank.party
Re: Reading and parsing a string
« Reply #7 on: April 14, 2011, 01:57:23 pm »
/smiley flood (lol)
Yep, an hex version would be useful. Wont know how to fit it in the axe code (cuz there are 2 different cases), though, so you should probably give some explaination.
« Last Edit: April 14, 2011, 02:02:43 pm by aeTIos »
I'm not a nerd but I pretend:

Ashbad

  • Guest
Re: Reading and parsing a string
« Reply #8 on: April 14, 2011, 02:04:51 pm »
Thing is, the routine would have to be changed around for it to be "slid" into axe code.  The first one is for Axe only -- the second one would have to have some way of knowing the current Axe address and the address of a later part to work in an axe program -- but it would be perfect in an assembly program :)

Offline aeTIos

  • Nonbinary computing specialist
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3915
  • Rating: +184/-32
    • View Profile
    • wank.party
Re: Reading and parsing a string
« Reply #9 on: April 14, 2011, 02:11:17 pm »
Yep, I see. Can't you change it to output either 0 or 1 and let the strings input in str1 and 2? Or is that too hard?
I'm not a nerd but I pretend:

Ashbad

  • Guest
Re: Reading and parsing a string
« Reply #10 on: April 14, 2011, 04:47:28 pm »
well, the input isn't really the hard part (It would be really simple actually as an axiom command) but the data flow and the allowing of stuff to work in order the way I have it.  However, this might be a simple solution once the next Axe version comes out with stack commands -- if you don't wanna wait, I can write you a an Axiom Version ;)

Code: ("Assembly") [Select]
  POP HL
  POP DE
  POP BC
  XOR A
Loop
  EX DE,HL
  LD A,(HL)
  EX DE,HL
  CPD
  JR NZ,NotSame
  DEC BC
  JP PE,Loop
Same
  INC A
NotSame
  LD H,0
  LD L,A
  PUSH HL

You could then access the command like this:

Code: ("Axe") [Select]
Length(Str2) -> n
Str2 -> n
Str1 -> n

Asm(the opcode)

n -> A

this would do the parsing and store 1 to A if the two strings are equal, and 0 to A if not. :)

EDIT: and I'm proud to say I was able to write those routines myself :) I don't feel so total noobish anymore in asm :D
« Last Edit: April 14, 2011, 04:48:29 pm by Ashbad »

Offline Quigibo

  • The Executioner
  • CoT Emeritus
  • LV11 Super Veteran (Next: 3000)
  • *
  • Posts: 2031
  • Rating: +1075/-24
  • I wish real life had a "Save" and "Load" button...
    • View Profile
Re: Reading and parsing a string
« Reply #11 on: April 14, 2011, 09:45:21 pm »
You do realize there was a new command added in 0.5.1 to compare string equality right ?  Equ>String(Str1,Str2)  ;)
___Axe_Parser___
Today the calculator, tomorrow the world!

Offline aeTIos

  • Nonbinary computing specialist
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3915
  • Rating: +184/-32
    • View Profile
    • wank.party
Re: Reading and parsing a string
« Reply #12 on: April 15, 2011, 06:42:03 am »
Cool! Just gotta get 0.5.1 on my calc!
I'm not a nerd but I pretend: