Click here to Skip to main content
15,888,461 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: I've really painted myself into a corner Pin
Ron Anders28-Dec-20 15:28
Ron Anders28-Dec-20 15:28 
GeneralRe: I've really painted myself into a corner Pin
Super Lloyd28-Dec-20 18:10
Super Lloyd28-Dec-20 18:10 
GeneralRe: I've really painted myself into a corner Pin
honey the codewitch28-Dec-20 18:23
mvahoney the codewitch28-Dec-20 18:23 
JokeRe: I've really painted myself into a corner Pin
W Balboos, GHB29-Dec-20 1:04
W Balboos, GHB29-Dec-20 1:04 
GeneralRe: I've really painted myself into a corner Pin
Sander Rossel29-Dec-20 3:17
professionalSander Rossel29-Dec-20 3:17 
GeneralRe: I've really painted myself into a corner Pin
honey the codewitch29-Dec-20 5:01
mvahoney the codewitch29-Dec-20 5:01 
GeneralWhen "Pro" meets Nerd Pin
Gerry Schmitz28-Dec-20 7:08
mveGerry Schmitz28-Dec-20 7:08 
GeneralRe: When "Pro" meets Nerd Pin
honey the codewitch28-Dec-20 7:18
mvahoney the codewitch28-Dec-20 7:18 
Sometimes people get tunnel vision. They think of a way to solve a problem, and what should have been one throwaway design iteration to be replaced with a better solution instead becomes a focal point and almost an obsession and then they spend the next two weeks coding an anti-pattern.

It has happened to me, and I've seen it happen to more programmers than I can count.

Besides, that code may be ugly, but it's not this: Laugh | :laugh:
C++
static bool lexNumber(lex::LexSource& ls,JsonLexState& st) {
    int32_t cp;
    if(!ls.ensureStarted())
        return false; // error
    
    switch(st.flags.state) {
        case 0:
            st.flags.accept=0;
            st.flags.eCount=0;
            st.flags.fracCount=0;
            st.flags.fracPart=0;
            st.flags.negative=0;
            st.flags.overflow=0;
            st.integer = 0;
            st.real = 0;
            if(ls.more()) {
                if (((cp=ls.current()) == '-')) {
                    if(!ls.capture(cp)) {
                        return false;
                    }
                    st.flags.negative =1;
                    if(!ls.advance()) {
                        if(ls.hasError())
                            return false;
                    }
                    st.flags.state = 1;
                    return true;
                }
                if ((cp == '0')) {
                    if(!ls.capture(cp)) {
                        return false;
                    }
                    if(!ls.advance()) {
                        if(ls.hasError())
                            return false;
                    }
                    st.flags.state = 2;
                    return true;
                }
                if (((cp >= '1') 
                            && (cp <= '9'))) {
                    if(!ls.capture(cp)) {
                        return false;
                    }
                    cp-='0';
                    st.integer=cp;
                    st.real = cp;
                    if(!ls.advance()) {
                        if(ls.hasError())
                            return false;
                    }
                    st.flags.state = 8;
                    return true;
                }
            }
            goto error;
        case 1:
            if(ls.more()) {
                if (((cp=ls.current()) == '0')) {
                    if(!ls.capture(cp)) {
                        return false;
                    }
                    cp-='0';
                    if(0!=st.flags.negative)
                            cp=-cp;
                    st.real=cp;
                    st.integer=cp;
                    if(!ls.advance()) {
                        if(ls.hasError())
                            return false;
                    }
                    st.flags.state = 2;
                    return true;
                }
                if (((cp >= '1') 
                            && (cp <= '9'))) {
                    if(!ls.capture(cp)) {
                        return false;
                    }
                    cp-='0';
                    if(0!=st.flags.negative)
                            cp=-cp;
                    st.real=cp;
                    st.integer=cp;
                    if(!ls.advance()) {
                        if(ls.hasError())
                            return false;
                    }
                    st.flags.state = 8;
                    return true;
                }
            }
            goto error;
        case 2:
            if(ls.more()) {
                if ((((cp=ls.current())== 'E') 
                            || (cp == 'e'))) {
                    if(!ls.capture(cp)) {
                        return false;
                    }
                    st.flags.fracPart = 2;
                    if(!ls.advance()) {
                        if(ls.hasError())
                            return false;
                    }
                    st.flags.state = 5;
                    return true;
                }
                if ((cp == '.')) {
                    if(!ls.capture(cp)) {
                        return false;
                    }
                    ++st.flags.fracCount;
                    st.flags.fracPart=1;
                    if(!ls.advance()) {
                        if(ls.hasError())
                            return false;
                    }
                    st.flags.state = 3;
                    return true;
                }
            }
            st.flags.accept=1;
            return false;
        case 3:
            if(ls.more()) {
                if ((((cp=ls.current()) >= '0') 
                            && (cp <= '9'))) {
                    if(!ls.capture(cp)) {
                        return false;
                    }
                    if(st.flags.fracCount<63) {
                        cp-='0';
                        if(0!=st.flags.negative)
                                cp=-cp;
                        st.real+=(cp*(pow(10,-st.flags.fracCount)));
                        ++st.flags.fracCount;
                    }
                    if(!ls.advance()) {
                        if(ls.hasError())
                            return false;
                    }
                    st.flags.state = 4;
                    return true;
                }
            }
            goto error;
        case 4:
            if(ls.more()) {
                if ((((cp=ls.current())== 'E') 
                            || (cp == 'e'))) {
                    if(!ls.capture(cp)) {
                        return false;
                    }
                    if(!ls.advance()) {
                        if(ls.hasError())
                            return false;
                    }
                    st.flags.state = 5;
                    return true;
                }
                if (((cp >= '0') 
                            && (cp <= '9'))) {
                    if(!ls.capture(cp)) {
                        return false;
                    }
                    if(st.flags.fracCount<63) {
                        cp-='0';
                        if(0!=st.flags.negative)
                                cp=-cp;
                        st.real+=(cp*(pow(10,-st.flags.fracCount)));
                        ++st.flags.fracCount;
                    }
                    if(!ls.advance()) {
                        if(ls.hasError())
                            return false;
                    }
                    st.flags.state = 4;
                    return true;
                }
            }
            st.flags.accept=2;
            return false; // orig
        case 5:
            if(ls.more()) {
                if ((((cp=ls.current()) == '+') 
                            || (cp == '-'))) {
                    if('-'==cp)
                        st.flags.fracPart = 3; // indicates neg exponent
                    if(!ls.capture(cp)) {
                        return false;
                    }
                    if(!ls.advance()) {
                        if(ls.hasError())
                            return false;
                    }
                    st.flags.state = 6;
                    return true;
                }
                if (((cp >= '0') 
                            && (cp <= '9'))) {
                    if(!ls.capture(cp)) {
                        return false;
                    }
                    cp-='0';
                    if(st.flags.eCount>6553) {
                        st.flags.overflow = 1;
                    }
                    st.flags.eCount=st.flags.eCount*10+cp;
                    if(!ls.advance()) {
                        if(ls.hasError())
                            return false;
                    }
                    st.flags.state = 7;
                    return true;
                }
            }
            goto error;
        case 6:
            if(ls.more()) {
                if ((((cp=ls.current()) >= '0') 
                            && (cp <= '9'))) {
                    if(!ls.capture(cp)) {
                        return false;
                    }
                    cp-='0';
                    if(st.flags.eCount>6553) {
                        st.flags.overflow = 1;
                    }
                    st.flags.eCount=st.flags.eCount*10+cp;
                    if(!ls.advance()) {
                        if(ls.hasError())
                            return false;
                    }
                    st.flags.state = 7;
                    return true;
                }
            }
            goto error;
        case 7:
            if(ls.more()) {
                if ((((cp=ls.current()) >= '0') 
                            && (cp <= '9'))) {
                    if(!ls.capture(cp)) {
                        return false;
                    }
                    cp-='0';
                    if(st.flags.eCount>6553) {
                        st.flags.overflow = 1;
                    }
                    st.flags.eCount=st.flags.eCount*10+cp;
                    if(!ls.advance()) {
                        if(ls.hasError())
                            return false;
                        
                    }
                    st.flags.state = 7;
                    return true;
                }
            }
            st.flags.accept=2;
            return false; // orig
        case 8:
            if(ls.more()) {
                if ((((cp=ls.current())== 'E') 
                            || (cp == 'e'))) {
                    if(!ls.capture(cp)) {
                        return false;
                    }
                    st.flags.fracPart = 2;
                    if(!ls.advance()) {
                        if(ls.hasError())
                            return false;
                    }
                    st.flags.state = 5;
                    return true;
                }
                if (((cp >= '0') 
                            && (cp <= '9'))) {
                    if(!ls.capture(cp)) {
                        return false;
                    }
                    cp=cp-'0';
                    if(st.flags.negative)
                        cp=-cp;
                    st.integer=st.integer*10+cp;
                    if((0>st.integer && 0==st.flags.negative)||(0<st.integer && 0!=st.flags.negative)) {
                        st.flags.overflow = 1;
                    }
                    st.real = floor(st.real*10.0+0.5)+cp;
                    if(!ls.advance()) {
                        if(ls.hasError())
                            return false;
                    }
                    st.flags.state = 8;
                    return true;
                }
                if ((cp == '.')) {
                    if(!ls.capture(cp)) {
                        return false;
                    }
                    ++st.flags.fracCount;
                    st.flags.fracPart=1;
                    if(!ls.advance()) {
                        if(ls.hasError())
                            return false;
                    }
                    st.flags.state = 3;
                    return true;
                }
            }
            st.flags.accept =1;
            return false;
    }
error:
    // TODO: figure out why not consistent capture on error
    ls.capture(ls.current());
    /*if(!ls.advance()) {
        return false;
    }*/
    return false;
}


That's a DFA generated from a regular expression into a C# state machine, I then ported to C++ and added numeric computations in the middle of it.

It's because strtod() doesn't stream and neither does atoll() or atoi() - you have to have the entire string loaded before you can parse, and my DFA above supports infinite precision in lexical space but imposes limits in value space, so that you can round trip numbers of any precision.

I can defend the code based on it originating from machine generated source, for which I have a graphical flow chart also that goes with this routine.

It's less buggy than writing it all by hand, despite how ugly it is.

So sometimes there's a reason for nasty code. Maybe there was a reason.
Real programmers use butterflies


modified 28-Dec-20 13:26pm.

GeneralRe: When "Pro" meets Nerd Pin
Gerry Schmitz28-Dec-20 17:31
mveGerry Schmitz28-Dec-20 17:31 
GeneralRe: When "Pro" meets Nerd Pin
#realJSOP28-Dec-20 10:19
mve#realJSOP28-Dec-20 10:19 
GeneralRe: When "Pro" meets Nerd Pin
Jörgen Andersson28-Dec-20 10:44
professionalJörgen Andersson28-Dec-20 10:44 
GeneralRe: When "Pro" meets Nerd Pin
Gerry Schmitz28-Dec-20 17:01
mveGerry Schmitz28-Dec-20 17:01 
GeneralRe: When "Pro" meets Nerd Pin
Daniel Pfeffer28-Dec-20 20:50
professionalDaniel Pfeffer28-Dec-20 20:50 
GeneralBrits, UK'ians Pin
W Balboos, GHB28-Dec-20 6:55
W Balboos, GHB28-Dec-20 6:55 
GeneralRe: Brits, UK'ians Pin
OriginalGriff28-Dec-20 7:35
mveOriginalGriff28-Dec-20 7:35 
GeneralRe: Brits, UK'ians Pin
W Balboos, GHB28-Dec-20 7:38
W Balboos, GHB28-Dec-20 7:38 
GeneralRe: Brits, UK'ians Pin
OriginalGriff28-Dec-20 7:42
mveOriginalGriff28-Dec-20 7:42 
GeneralRe: Brits, UK'ians Pin
Daniel Pfeffer28-Dec-20 8:23
professionalDaniel Pfeffer28-Dec-20 8:23 
GeneralRe: Brits, UK'ians Pin
W Balboos, GHB28-Dec-20 8:26
W Balboos, GHB28-Dec-20 8:26 
GeneralRe: Brits, UK'ians Pin
Daniel Pfeffer28-Dec-20 8:55
professionalDaniel Pfeffer28-Dec-20 8:55 
GeneralRe: Brits, UK'ians Pin
W Balboos, GHB29-Dec-20 1:44
W Balboos, GHB29-Dec-20 1:44 
GeneralRe: Brits, UK'ians Pin
Slow Eddie29-Dec-20 2:53
professionalSlow Eddie29-Dec-20 2:53 
GeneralRe: Brits, UK'ians Pin
W Balboos, GHB29-Dec-20 3:09
W Balboos, GHB29-Dec-20 3:09 
GeneralRe: Brits, UK'ians Pin
Mircea Neacsu28-Dec-20 7:54
Mircea Neacsu28-Dec-20 7:54 
GeneralRe: Brits, UK'ians Pin
5teveH28-Dec-20 22:09
5teveH28-Dec-20 22:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.