Skip to content

Instantly share code, notes, and snippets.

@hcchengithub
Last active October 29, 2021 08:50
Show Gist options
  • Save hcchengithub/6da91898d2c7604ec3bb4a06245d1e37 to your computer and use it in GitHub Desktop.
Save hcchengithub/6da91898d2c7604ec3bb4a06245d1e37 to your computer and use it in GitHub Desktop.
modify the file that were just created
{"updated content":"from curl and modified "}
{
"Windows":["windows?.?10","win.?10"],
"Bluetooth":["Blue.?tooth","BT"]
}
import peforth
peforth.dictate(r'''
\ Rev-13 Jupyter notebook %load https://gist.githubusercontent.com/hcchengithub/6da91898d2c7604ec3bb4a06245d1e37/raw/words4jupyter.py
\ 修改 peforth 處理 unknown token 的方式,讓它認得 jupyter notebook 下的 globals, locals.
none value _locals_ // ( -- dict ) locals passed down from ok()
false value debug // ( -- flag ) enable/disable the ok() breakpoint
: unknown ( token -- thing Y|N) // Try to find the unknown token in __main__ or _locals_
_locals_ if \ in a function
( token ) _locals_ :> get(tos(),"Ûnknôwn") ( token, local )
py> str(tos())!="Ûnknôwn" ( token, local, unknown? )
if ( token, local ) nip true exit ( return local Y ) else drop ( token ) then
then
( token ) py> getattr(sys.modules['__main__'],pop(),"Ûnknôwn") ( thing )
py> str(tos())=="Ûnknôwn" if ( thing ) drop false else true then ;
/// Example: Set a breakpoint in python code like this:
/// if peforth.execute('debug').pop() : peforth.push(locals()).ok("bp>",cmd='to _locals_')
/// Example: Save locals for investigations:
/// if peforth.execute('debug').pop() : peforth.push(locals()).dictate('to _locals_')
/// That enters peforth that knows variables in __main__ and locals at the breakpoint.
/// 'exit' to leave the breakpoint and forget locals.
: exit ( -- ) // ( -- ) Exit the breakpoint forget locals and continue the process
none to _locals_ py: vm.exit=True ;
code # print(nexttoken('\n')+'\n') end-code // print the comment line after #
: --> ( result -- ) // Print the result with the command line.
py> tib[:ntib].rfind("\n") py> tib[max(pop(),0):ntib].strip() ( result cmd-line )
s" {} {} ({})" :> format(pop(),tos(),type(pop())) . cr ;
/// Good for experiments that need to show command line and the result.
: ( ( <str> -- ) // Comment down to ')' which can be nested if balanced
py> nextstring('\(|\)')['str'] \ word 固定會吃掉第一個 character 故不適用。
drop py> tib[ntib] py: vm.ntib+=1 \ 撞到停下來的字母非 '(' 即 ')' 要不就是行尾,都可以 skip 過去
char ( = if \ 剛才那個字母是啥?
[ last literal ] dup \ 取得本身
execute \ recurse nested level
execute \ recurse 剩下來的部分
then ; immediate
code list push(list(pop())) end-code // ( sth -- list ) Cast something into a list
code dict push(dict(pop())) end-code // ( sth -- dict ) Cast something into a dictionary
code str push(str(pop())) end-code // ( sth -- str ) Cast something into a string
code set push(set(pop())) end-code // ( sth -- set ) Cast something into a set
code tuple push(tuple(pop())) end-code // ( sth -- tuple ) Cast something into a tuple
: round-off ( f 100 -- f' ) // 對 f 取小數點以下 2 位四捨五入
py> int(pop(1)*tos(0)+0.5)/pop(0) ;
code % push(pop(1)%pop(0)) end-code // ( a b -- a%b )
code txt2json # ( txt -- dict ) Convert given string to dictionary
push(json.loads("".join([ c if c != "'" else '"' for c in pop()])))
end-code
// ( txt -- dict ) Convert given string to dictionary
marker ===
''');
@hcchengithub
Copy link
Author

Revision 13 Improve 'unknown' comment

Add a better usage to save locals for future investigations at global namespace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment