// 2011.02.02 # Doj
// Команды для работы с локальными переменными

include" units\utils\parser.de"

0 variable *local-offset
8 bdyn *local-names

: local-offset>> *local-offset dup @ cell+ swap ! ;
: local-offset-off *local-offset off ;

// : dup dup ;

: local-created ( sp) str+ created immediate *local-offset @ , ;
: put-local-offset ( p-) @ call literal ;
: local-generated! ( s-) " !" local-created does> put-local-offset compile l! ;
: local-generated@ ( s-) " @" local-created does> put-local-offset compile l@ ;
: local-++generated ( s-) " ++" bswap local-created 
  does> dup put-local-offset 
            compile l@ 
            compile 1+ 
            compile dup
            put-local-offset 
            compile l! 
;
: local-generated++ ( s-) " ++" local-created 
  does> dup put-local-offset 
            compile l@ 
            compile dup 
            compile 1+
            put-local-offset 
            compile l! 
;
: local---generated ( s-) " --" bswap local-created 
  does> dup put-local-offset 
            compile l@ 
            compile 1- 
            compile dup
            put-local-offset 
            compile l! 
;
: local-generated-- ( s-) " --" local-created 
  does> dup put-local-offset 
            compile l@ 
            compile dup 
            compile 1-
            put-local-offset 
            compile l! 
;
: local-generated1+ ( s-) " 1+" local-created 
  does> dup put-local-offset 
            compile l@ 
            compile 1+
            put-local-offset 
            compile l! 
;
: local-generated1- ( s-) " 1-" local-created 
  does> dup put-local-offset 
            compile l@ 
            compile 1-
            put-local-offset 
            compile l! 
;
: local-generated+ ( s-) " +" local-created 
  does>     put-local-offset 
            compile l@ 
            compile +
;
: local-generated- ( s-) " -" local-created 
  does>     put-local-offset 
            compile l@ 
            compile -
;
: local-generated* ( s-) " *" local-created 
  does>     put-local-offset 
            compile l@ 
            compile *
;
: local-generated+! ( s-) " +!" local-created 
  does> dup put-local-offset 
            compile l@ 
            compile +
            put-local-offset
            compile l!
;
: local-generated-! ( s-) " -!" local-created 
  does> dup put-local-offset 
            compile l@ 
            compile -
            put-local-offset
            compile l!
;
: local-generated*! ( s-) " *!" local-created 
  does>     put-local-offset 
            compile l@ 
            compile *
            put-local-offset
            compile l!
;

: local-generated-int ( s-)
  bdup local-generated!
  bdup local-generated@
  bdrop
;

: prepare-local-name? ( B: x -- f B: y) bdup str0 str= not ;

: local-generated-cell ( varname -- flag)
  prepare-local-name? not if false exit then
  bdup local-generated!
  bdup local-generated@
  bdup local-generated1+
  bdup local-generated1-
  bdup local-++generated
  bdup local-generated++
  bdup local---generated
  bdup local-generated--
  bdup local-generated+
  bdup local-generated-
  bdup local-generated*
  bdup local-generated+!
  bdup local-generated-!
  bdup local-generated*!
  bdrop true
;

: parse-locals-list ( B: s -) *local-names "  " parse-list ;

: generate-all-locals
  *local-names bdyn# begin dup 0> while 1-
    dup *local-names bdyn[]@ local-generated-cell if local-offset>> then
  repeat drop 
;

: initialiaze-all-locals
  *local-offset off *local-names bdyn# begin dup 0 > while 1-
    dup *local-names bdyn[]@ prepare-local-name?
    if *local-offset @ 
      // ['] literal execute 
      call literal
      compile l! 
      local-offset>>
    then bdrop
  repeat drop 
;

: 0initialiaze-all-locals
  *local-offset off *local-names bdyn# begin dup 0 > while 1-
    dup *local-names bdyn[]@ prepare-local-name?
    if *local-offset @ 
      // ['] literal execute 
      0 call literal
      call literal
      compile l! 
      local-offset>>
    then bdrop
  repeat drop 
;

: get-memory-for-locals *local-offset @ 0; ['] literal execute compile l+ ;

: x|ed ( B: s --)  
  compile branch >mark 
    local-offset-off
    *local-names bdyn.clear
    parse-locals-list
    generate-all-locals 
  >resolve 
  get-memory-for-locals
;

: |ed x|ed initialiaze-all-locals ;
: 0|ed x|ed 0initialiaze-all-locals ;

: | ( "x1 x2 .. xn|"-) " |" source-cut |ed ; immediate
: 0| ( "x1 x2 .. xn|"-) " |" source-cut 0|ed ; immediate
: x| ( "x1 x2 .. xn|"-) " |" source-cut x|ed ; immediate

// добавляем при завершении определения команды обнуление локальной переменной
: ; postpone ; local-offset-off ; immediate
