1
0
kolibrios/programs/develop/tinypy/tp_kolibri.txt
jaeger c27ead6711 tinypy: can now run from shell and use command line parameters.
git-svn-id: svn://kolibrios.org@2073 a494cfbc-eb01-0410-851d-a64ba20cac60
2011-08-18 21:08:16 +00:00

152 lines
5.7 KiB
Plaintext
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

Ž¡§®à ¨­â¥à¯à¥â â®à  TinyPy ¨ § ¬¥âª¨ ¯® ¥£®  ¤ ¯â æ¨¨ ª à ¡®â¥ ¢ Š®«¨¡à¨Ž‘.
1. —â® â ª®¥ TinyPy ¨ 祬 ®­ å®à®è ¢ ¯« ­¥ ¯®àâ¨à®¢ ­¨ï
TinyPy - ¨­â¥à¯à¥â â®à Python-¯®¤®¡­®£® ï§ëª , ᮧ¤ ­­ë© ”¨«®¬
•íáᨠ(Phil Hassey), ®â«¨ç î騩áï ®ç¥­ì ­¥¡®«ì訬¨ à §¬¥à ¬¨: ¢ë¡®à®¬ ®¯æ¨©
ª®¬¯¨«ï樨 ¬®¦­® ¤®¡¨âìáï à §¬¥à®¢ ¨á¯®«­ï¥¬®£® ¬®¤ã«ï ¢ ¯à¥¤¥« å 64 ª¨«®¡ ©â.
<20>ਠࠧࠡ®âª¥ ¯à¨«®¦¥­¨© ¤«ï Š®«¨¡à¨ ¢ ¤àã£¨å ®¯¥à æ¨®­­ëå á¨á⥬ å
¤®áâ㯥­ ¤®¢®«ì­® è¨à®ª¨© ¢ë¡®à ªà®áá-ª®¬¯¨«ïâ®à®¢. <20> ¡®à á।á⢠ࠧࠡ®âª¨ ¢
á ¬®© Š®«¨¡à¨ ¤® ­¥¤ ¢­¥£® ¢à¥¬¥­¨ ¡ë« ®£à ­¨ç¥­ FASM. Œ®¦­® ¯à¥¤¯®«®¦¨âì, çâ®
¢®§¬®¦­®áâì ᮧ¤ ¢ âì ¯à®£à ¬¬ë ¢ á ¬®© á¨á⥬¥ ­  â ª®¬ «¥£ª®¨§ãç ¥¬®¬ ¨
à á¯à®áâà ­ñ­­®¬ ï§ëª¥, ª ª Python, ¯®¢ë蠥⠯ਢ«¥ª â¥«ì­®áâì á¨áâ¥¬ë ¤«ï
¯®«ì§®¢ â¥«ï, ¤ «ñª®£® ®â ¯à®£à ¬¬¨à®¢ ­¨ï ­   áᥬ¡«¥à¥.
TinyPy ®ª § «áï «ñ£®ª ¢ ¯®àâ¨à®¢ ­¨¨: §­ ç¨â¥«ì­ ï ç áâì ¥£® ­ ¯¨á ­  ­  á ¬®¬
Python,   ®áâ «ì­ ï ç áâì - ­  C, ¨á室­ë¥ ⥪áâë § ­¨¬ îâ çãâì ¡®«ìè¥ 200 Š¡.
„«ï ª®¬¯¨«ï樨 ­ã¦­  ⮫쪮 áâ ­¤ àâ­ ï ¡¨¡«¨®â¥ª  libc. ˆ§ ­¥ñ ¨á¯®«ì§ã¥âáï
¢á¥£® ®ª®«® ¯®«ãâ®à  ¤¥áï⪮¢ ä㭪権 (vsnprintf/vsprintf, malloc/memcpy/memmove/free,
fopen/fread/fwrite/fclose).
¨â®£¥ à ¡®â  ¯® ¯®àâ¨à®¢ ­¨î TinyPy á®áâ®ï«  ¨§ á«¥¤ãîé¨å ç á⥩:
1. <20> ¯¨á ­¨¥ Makefile ¤«ï ᡮન ª®¬¯¨«ïâ®à®¬ GCC á ¨á¯®«ì§®¢ ­¨¥¬
menuetlibc.
2. „®¡ ¢«¥­¨¥ ¢ menuetlibc ­¥¤®áâ îé¨å ä㭪権 vsnprintf/vsprintf.
3. <20>®¤ª«î祭¨¥ ¡¨¡«¨®â¥ª¨ Š®«¨¡à¨ ¤«ï à ¡®âë á ª®­á®«ìî.
4. ˆ ª®­¥ç­®, ®â«®¢ à §­®®¡à §­ëå ¬¥«ª¨å ¡ £®¢ (ª ª ¦¥ ¡¥§ í⮣®!).
2. ‚®§¬®¦­®á⨠TinyPy.
2.1. ’¨¯ë ¤ ­­ëå.
Š ¦¤ë© ®¡ê¥ªâ ¢ á।¥ TinyPy ¯à¥¤áâ ¢«ï¥âáï 16-¡ ©â®¢®© áâàãªâãன tp_obj.
<EFBFBD>¥à¢ë¥ 4 ¡ ©â  § ­¨¬ ¥â ⨯, ¨ ­ §­ ç¥­¨¥ ¯®á«¥¤ãîé¨å 12 ¡ ©â § ¢¨á¨â ®â ­¥£®.
„®áâ㯭ë á«¥¤ãî騥 ⨯ë: TP_NONE, TP_NUMBER, TP_STRING, TP_DICT, TP_LIST,
TP_FNC, TP_DATA.
TP_NONE - ᮮ⢥âáâ¢ã¥â ¥¤¨­á⢥­­®¬ã ®¡ê¥ªâã None. <20>®«ìè¥ ¯à® ­¥£® ᪠§ âì
­¥ç¥£®.
TP_NUMBER - ç¨á«®, åà ­¨âáï ª ª float. …᫨ ç¨á«® ®â«¨ç ¥âáï ®â ¡«¨¦ ©è¥£®
楫®£® ¬¥­ìè¥, 祬 ­  10^(-6), ®­® áç¨â ¥âáï 楫ë¬, ¨ ¯à¨ ¯¥ç â¨ ¢ë¢®¤¨âáï
ª ª 楫®¥. ’ ª¦¥ ª í⮬ã ⨯㠮⭮áïâáï «®£¨ç¥áª¨¥ True ¨ False.
<20>®¤¤¥à¦¨¢ îâáï  à¨ä¬¥â¨ç¥áª¨¥ ®¯¥à æ¨¨ +,-,*,/,%, ¡¨â®¢ë¥ << ¨ >>, «®£¨ç¥áª¨¥
|, &.
<20>ਬ¥à:
print(2+2*2, 7/3, (-7)%3, 1==True, 0==False)
‚뢮¤¨â:
6 2.333333 -1 1 1
‡ ¬¥â¨¬ áà §ã, çâ® print ï¥âáï ä㭪樥© (ª ª ¢ Python 3), â® ¥áâì ¯ à ¬¥âàë
®¡ï§ â¥«ì­® 㪠§ë¢ âì ¢ ᪮¡ª å.
TP_STRING - áâப . <20>®¤¤¥à¦¨¢ îâáï: ¯à®¢¥àª  ¢å®¦¤¥­¨ï ("str1" in "str2"), á१ë s[:b], s[a:b],
s[a:], ä㭪樨 len, index, join, split, find, strip, replace.
TP_DICT - á«®¢ àì, ®­ ¦¥  áá®æ¨ â¨¢­ë© ¬ áᨢ. <20>®¤¤¥à¦¨¢ ¥âáï ¤®¡ ¢«¥­¨¥
­®¢®£® í«¥¬¥­â  ¨ ¢á¥å í«¥¬¥­â®¢ ¤à㣮£® á«®¢ àï. <20>¥à¥¡®à ¯® ¢á¥¬ ª«îç ¬ ¨
㤠«¥­¨¥ í«¥¬¥­â®¢ ¯®ª  ­¥ ¯®¤¤¥à¦¨¢ ¥âáï. ‘«®¢ à¨ â ª¦¥ ïîâáï 㤮¡­ë¬ ᯮᮡ®¬
¯à¥¤áâ ¢«¥­¨ï ®¡ê¥ªâ®¢, ¢ ç áâ­®áâ¨, ¥á«¨ d á«®¢ àì, â® § ¯¨á¨ d.key ¨
d['key'] à ¢­®§­ ç­ë. í⮬ TinyPy ­¥¬­®£® ­ ¯®¬¨­ ¥â JavaScript.
TP_LIST - ᯨ᮪. <20>®¤¤¥à¦¨¢ îâáï: ¯¥à¥¡®à ¯® í«¥¬¥­â ¬ (for el in list),
ä㭪樨 len, append/appendx, extend, insert/insertx, pop, find, index, sort.
’ ª¦¥ ¯®¤¤¥à¦¨¢ ¥âáï äã­ªæ¨ï range. reverse() ¯®ª  ­¥ ¯®¤¤¥à¦¨¢ ¥âáï.
TP_FNC - äã­ªæ¨ï. <20>뢠¥â 2 ⨯®¢ - ®¡ëç­ ï äã­ªæ¨ï ¨ ¬¥â®¤, ®â«¨ç îâáï
¯®à浪®¬ ¢ë§®¢ .
TP_DATA - ¢­ãâ७­¨¥ áâàãªâãàë TinyPy.
2. ‚®§¬®¦­®á⨠TinyPy ¢ áà ¢­¥­¨¨ á "¡®«ì訬" Python
"what tinypy won't be:
- a full implementation of Python;
- totally compatible with Python"
"roadmap.txt", tinypy sources
‘¨­â ªá¨á ï§ëª  TinyPy ®ç¥­ì ¯®å®¦ ­  Python, ­® ¬­®£¨¥ áãé¥á⢥­­ë¥
¢®§¬®¦­®á⨠Python ­¥ ¯®¤¤¥à¦¨¢ îâáï:
- ¨â¥à â®àë ¨ ᮯணࠬ¬;
- ᨭ⠪á¨ç¥áª¨© "á å à" ¤«ï ®¡ê¥­¨ï ᯨ᪮¢ ¨ á«®¢ à¥©, ­ ¯®¤®¡¨¥
odd_squares = [a*a for a in range(100) if a%2];
- ¨­â¥à ªâ¨¢­ ï ª®­á®«ì.
ˆ ª®­¥ç­®, ­¥ 墠⠥⠮᭮¢­®© ᨫë Python - ¥£® áâ ­¤ àâ­®© ¡¨¡«¨®â¥ª¨.
3. <20> áè¨à¥­¨¥ TinyPy.
<20> ¡®à ¢áâ஥­­ëå ä㭪権 ¢ TinyPy ¤®áâ â®ç­® ¡¥¤¥­, ­® § â® ¤®¡ ¢«¥­¨¥ ­®¢ëå
¬®¤ã«¥© ¤®áâ â®ç­® ¯à®áâ®. <20> áᬮâਬ ᮧ¤ ­¨¥ ¬®¤ã«¥© ­  á ¬®¬ TinyPy, ­ 
C ¨ ­  FASM.
3.1. Œ®¤ã«¨ ­  TinyPy.
TinyPy ¯®¤¤¥à¦¨¢ ¥â ¬®¤ã«ì­®¥ ¯à®£à ¬¬¨à®¢ ­¨¥, ᮧ¤ ­¨¥ ¬®¤ã«¥©
¯à¨­æ¨¯¨ «ì­® ­¨ç¥¬ ­¥ ®â«¨ç ¥âáï ®â Python. <20>ãáâì, ã ­ á ¥áâì ¯à®áâ ï
¯à®£à ¬¬  ¨§ ¤¢ãå ä ©«®¢:
” ©« math.py:
def square(a):
return a*a
” ©« prog.py:
import math
if __name__=="__main__":
math.square(12)
‚­ ç «¥ ­ã¦­® ᪮¬¯¨«¨à®¢ âì ¬®¤ã«ì math.py ¢ ¡ ©â-ª®¤ ¯à¨ ¯®¬®é¨ ¯à®£à ¬¬ë
py2bc.py, ®­  à ¡®â ¥â ª ª ¢ Python, â ª ¨ ¢ TinyPy. ‡ ¯ã᪠¥¬ shell ¨ ¢ë¯®«­ï¥¬ ª®¬ ­¤ã
# tinypy py2bc.py math.py math.tpc
<20>®á«¥ í⮣® ¬®¦­® § ¯ãáâ¨âì prog.py ¨§ TinyPy.
# tinypy prog.py
144
‡ ¬¥ç ­¨¥: ᨭ⠪á¨á "from module import function" ¯®ª  ­¥ ¯®¤¤¥à¦¨¢ ¥âáï.
3.2 Œ®¤ã«¨ ­  C.
<20>ਬ¥à ᮧ¤ ­¨ï ¬®¤ã«¥© ­  C ¬®¦­® ¯®á¬®âà¥âì ¢ ª â «®£¥ modules/kolibri. ’ ª¨¥
¬®¤ã«¨ áâ â¨ç¥áª¨ «¨­ªãîâáï á á ¬¨¬ ¨­â¥à¯à¥â â®à®¬ ¨, ᮮ⢥âá⢥­­®,
㢥«¨ç¨¢ îâ ¥£® à §¬¥à. „«ï á­¨¦¥­¨ï ­ ª« ¤­ëå à á室®¢ ª®¬¯®­¥­âë TinyPy
ª®¬¯¨«¨àãîâáï ¢ ®¤¨­ ®¡ê¥ªâ­ë© ¬®¤ã«ì,   ä ©«ë ¨á室­ëå ⥪á⮢ ¢ª«îç îâáï á
¯®¬®éìî ¤¨à¥ªâ¨¢ë #include. „®¯®«­¨â¥«ì­ë¥ ¬®¤ã«¨ ¬®£ãâ ª®¬¯¨«¨à®¢ âìáï ª ª ¢
®â¤¥«ì­ë¥ ®¡ê¥ªâ­ë¥ ä ©«ë, ¢ í⮬ á«ãç ¥ ­ã¦­® ¤®¯¨á âì ¨å ¢ Makefile, ¨«¨
â®ç­® â ª ¦¥ ¯®¤ª«îç âìáï ¤¨à¥ªâ¨¢®© include.
¬®¤ã«¥ ¤®«¦­  ¯à¨áãâá⢮¢ âì äã­ªæ¨ï ¨­¨æ¨ «¨§ æ¨¨ ®¡ëç­® ®­  ­ §ë¢ ¥âáï
<modname>_init. Ž­  ¢ë§ë¢ ¥âáï ¢ tpmain áࠧ㠯®á«¥ ¨­¨æ¨ «¨§ æ¨¨ ¢¨àâã «ì­®©
¬ è¨­ë, ᮧ¤ ñâ á«®¢ àì íªá¯®àâ¨à㥬ëå ®¡ê¥ªâ®¢ ¨ ¤®¡ ¢«ï¥â ¥£® ¢ ¯à¥¤®¯à¥¤¥«ñ­­ë© á«®¢ àì modules.
—â®¡ë ®¯à¥¤¥«¨âì TinyPy-äã­ªæ¨î, ­ã¦­® ᮧ¤ âì ®¡ëç­ãî äã­ªæ¨î ­  C ¢¨¤ 
tp_obj myfunc(tp_vm *tp);
3.3 Œ®¤ã«¨ ­  FASM.
Œ®¤ã«ì ­  FASM ¯¨è¥âáï  ­ «®£¨ç­®, ­® ¢á¥£¤  ª®¬¯¨«¨àã¥âáï ¢ ®â¤¥«ì­ë©
®¡ê¥ªâ­ë© ä ©«. <20>ਬ¥à ¬®¦­® ¯®á¬®âà¥âì ¢ ª â «®£¥ fasm_modules. Œ®¤ã«ì
kolibri_dbg íªá¯®àâ¨àã¥â ®¤­ã äã­ªæ¨î debug_print, ¢ë¢®¤ïéãî áâப㠭  ¤®áªã
®â« ¤ª¨.
4. Ž¡§®à ¬®¤ã«ï kolibri.
<20>த®«¦¥­¨¥ á«¥¤ã¥â.