Author Topic: scope  (Read 7266 times)

0 Members and 1 Guest are viewing this topic.

pber

  • Guest
scope
« on: July 02, 2014, 08:36:44 AM »
In the example c2 does not see variable scoped.
Don't undersand because not 100.

Code: [Select]
class Compiler
    method constructor(string src)
        sys p= compile src: call p
    end method
end class

int glob= 9
scope
    int scoped= 99

    new Compiler c1("glob++")
    new Compiler c2("scoped++")

    print glob cr   ' 10
    print scoped cr ' 99
end scope

Peter

  • Guest
Re: scope
« Reply #1 on: July 02, 2014, 09:56:25 AM »
Hello Paolo,

Code: [Select]
class Compiler
    method constructor(string src)
        sys p= compile src: call p
    end method
end class

int glob= 9
'scope
    int scoped=99

    new Compiler c1("glob++")
    new Compiler c2("scoped++")

    print glob    ' 10
    print scoped  ' 100
'end scope

pber

  • Guest
Re: scope
« Reply #2 on: July 02, 2014, 10:01:22 AM »
Hi Peter,
as you noticed scope is responsible.
But why?
It seems to me that the instance c2 should has rights
to see what is scoped where it works and/or where it is created.

isn't it?

pber

  • Guest
Re: scope
« Reply #3 on: July 02, 2014, 10:03:42 AM »
oops... was not you serious? :-)

Peter

  • Guest
Re: scope
« Reply #4 on: July 02, 2014, 10:25:37 AM »
Quote
oops... was not you serious?

Sorry Paolo, only little bit!  ;D

Peter

  • Guest
Re: scope
« Reply #5 on: July 02, 2014, 10:30:52 AM »
Here, another bad thing.

Code: [Select]
indexbase 0

Class Exponent
    single Tab[20]
   
    Method List(single a)
        static sys i
        Tab[i] = a
        i +=1
    End Method

    Method Show(sys a) as single
        Return Tab[a]
    End Method
   
End Class

Exponent ex

a= pow(5,0) '1
ex.List a

a= pow(5,1) '5
ex.List a

a= pow(5,2) '25
ex.List a

a= pow(5,-2) '0,04
ex.List a

a= pow(-2,-3) '-0.125
ex.List a

For i=0 to 4
    e = ex.show i
    print e
Next

Charles Pegge

  • Guest
Re: scope
« Reply #6 on: July 02, 2014, 11:52:48 AM »
Hi Paolo,

Interesting case!

The dynamic compiler cannot see the symbol scoped, It is no longer valid at the end of static compiling, since it has fallen out of scope. (The compilation fails, and the binary is set to a ret instruction.)

Scoping with dynamic compiling is a contingency I have not anticipated.

The dynamic compiler has access to all global variables which are valid at the end of static (main) compiling, and also the local/static variables in the procedure, defined above the position where dynamic compiling takes place.

Code: [Select]
class Compiler
    method constructor(string src)
        sys p= compile src
        er=error
        if er then
          print "Dynamic compiler " + er + src
        else
          call p
        end if
    end method
end class

int glob= 9
scope
    int scoped= 99
    new Compiler c1("glob++")
    new Compiler c2("scoped++")
    print glob    ' 10
    print scoped  ' 99
end scope
« Last Edit: July 02, 2014, 12:35:34 PM by Charles Pegge »

Charles Pegge

  • Guest
Re: scope
« Reply #7 on: July 02, 2014, 12:07:33 PM »

Hi Peter,

To avoid precision errors showing with floats, you can limit the number of decimal places:

print str(e,6)

Any other bads?

pber

  • Guest
Re: scope
« Reply #8 on: July 02, 2014, 01:51:29 PM »
The dynamic compiler cannot see the symbol scoped, It is no longer valid at the end of static compiling, since it has fallen out of scope. (The compilation fails, and the binary is set to a ret instruction.)

Clear.
I'll keep more careful, thanks.

pber

  • Guest
Re: scope
« Reply #9 on: July 02, 2014, 01:52:08 PM »
Quote
oops... was not you serious?

Sorry Paolo, only little bit!  ;D

 :)

pber

  • Guest
Re: scope
« Reply #10 on: July 08, 2014, 08:47:21 AM »
Charles: this program works with exo2.exe,
but not with Oxygen.dll nor RTL32.inc.

In latter 2 use cases dynamically compiled code
does not see the global mySys.

Code: [Select]
'#file       "xfoo.exe"
'$filename   "xfoo.exe"
'include     "f:\apps\basic\OxygenBasic_2\inc\RTL32.inc"

include once "console.inc"

class CodeBlock
   
    int compiled
    bstring src
    sys p
    string err
   
    method constructor(string src0)
        init src0
    end method

    method destructor()
        frees src
        if compiled then freememory p
    end method
   
    method init(bstring src0)
        this.src= src0
        'p_compile
    end method

    method p_compile()
        if not compiled
            p= compile src
            err= error
        end if
        if err then
            print "error " err cr
        else
            compiled=-1
        end if
    end method
   
    method run()
        p_compile
        if not this.err then
            call p
        end if
    end method
end class

class System
    int state
    method m()
        print "System::m()" cr
    end method
    method constructor()
    end method
end class

new System mySys

new CodeBlock cb("mySys.state=123")
cb.run
print mySys.state cr

new CodeBlock cb("mySys.m")
cb.run

waitkey

.

Charles Pegge

  • Guest
Re: scope
« Reply #11 on: July 08, 2014, 12:07:38 PM »
Hi Paolo,

I am not sure why it works at all. mysys is global but does not exist, lexically speaking, until it is created with the line: new system mysys.. So the compiler method will not see it.








pber

  • Guest
Re: scope
« Reply #12 on: July 08, 2014, 12:34:48 PM »
Hi Charles,

This is one of my attempts to work on the issue:
Code: [Select]
dim mySys as System*
new System mySys
...but it does not help.

Maybe there is some scope-related
behaviors I do not understand?

Charles Pegge

  • Guest
Re: scope
« Reply #13 on: July 08, 2014, 12:52:55 PM »
We may be going up the wrong path here. If you can give me a brief overview of what you want to achieve, I may be able to find better matching solutions.

pber

  • Guest
Re: scope
« Reply #14 on: July 08, 2014, 02:47:20 PM »
Thanks Charles,
but I argue I'm wrong because I'm not clear.

running programs with exo2.exe do not give me the problem.

running programs in exe form gives me the problem,
(both exe forms: Oxygen.dll and RTL'ed)

Which problem:
dynamically compiled code does not see global objects.

Your ... (deleted?) suggestion still suffers the problem.