Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Christopher Reimer Newsgroups: comp.lang.python Subject: Re: Creating a calculator Date: Fri, 01 Jul 2016 06:19:00 -0700 Lines: 55 Message-ID: References: <0E77E5BC-E8CC-4951-B520-37BF2E596D3A@icloud.com> <81839B88-3F2B-4E28-9E59-F4B1AAAF058D@icloud.com> Mime-Version: 1.0 (1.0) Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de p2ft7RNWEPLKn0l3EOVAeQWIXCELljtCQl/MyYImG6Og== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'broken': 0.03; 'interpreter.': 0.07; 'trailing': 0.07; 'cc:addr:python-list': 0.09; 'expected.': 0.09; 'interpreter,': 0.09; 'unpack': 0.09; 'syntax': 0.13; 'ignore': 0.14; 'interpreter': 0.15; 'crashes': 0.16; 'element.': 0.16; 'keyword,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'spurious': 0.16; 'try/except': 0.16; 'valid.': 0.16; 'written.': 0.16; 'wrote:': 0.16; 'later': 0.16; 'element': 0.18; 'integer': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'strip': 0.22; 'cc:no real name:2**0': 0.22; 'am,': 0.23; 'component': 0.23; 'ignored.': 0.23; 'split': 0.23; 'this:': 0.23; 'written': 0.24; "i've": 0.25; "doesn't": 0.26; 'chris': 0.26; 'least': 0.27; 'received:17': 0.27; 'end,': 0.29; 'spaces': 0.29; 'convert': 0.29; 'raise': 0.29; 'print': 0.30; 'code': 0.30; 'normally': 0.30; 'received:10.0.0': 0.32; 'statement': 0.32; 'run': 0.33; 'source': 0.33; 'third': 0.33; 'quotes': 0.33; 'correctly': 0.34; 'file': 0.34; 'received:10.0': 0.34; 'list': 0.34; 'could': 0.35; 'but': 0.36; 'needed': 0.36; 'basic': 0.36; 'keyword': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'charset:us-ascii': 0.37; 'list.': 0.37; "won't": 0.38; 'several': 0.38; 'end': 0.39; 'test': 0.39; 'still': 0.40; 'header :MIME-version:1': 0.60; 'enclosed': 0.63; 'strange': 0.63; 'more': 0.63; 'different': 0.63; 'jul': 0.72; '1991': 0.84; 'dialect': 0.84; 'header:In-reply-to:1': 0.84 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2016-07-01_04:,, signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 clxscore=1015 suspectscore=0 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1510270003 definitions=main-1607010134 X-Mailer: iPhone Mail (13F69) In-reply-to: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=4d515a; t=1467379142; bh=nZeXWbOQjkNIma1EvPlm5J6lPCcyWd9t/1TZRiDZKss=; h=Content-type:MIME-version:Subject:From:Date:Message-id:To; b=j4tdjKF/nJXy9ly5tZXTKv60jwRPAXSI45TyzyV6t3sdYq6Xy5fZ6s+vHo/Bji5CD hK6rN+wc8s7SJtRVusv4WUuF0gyZe3gKSBRmtOLfk5LxZUZ+8qyzC8uAa+44t8fhfM e/XTrnLxUBEWGHUVUATouik1O6fdMOZefadW/LghqPATMvHPTv7N+dUytYbTtn8rb3 eaVUif1G0w/k7EnxD9zoTezvbVZss22ZrvDz/kxcr2ZyTVR9sDXyB63udGNZLgf8KI RKaMoAp07/P28KpV/xd+te65PkEyZKm/EXiPqUBDvYi7bi37tQxvajQe4oDSFpiw9u ilZQqZ3bDWdIw== X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <81839B88-3F2B-4E28-9E59-F4B1AAAF058D@icloud.com> X-Mailman-Original-References: <0E77E5BC-E8CC-4951-B520-37BF2E596D3A@icloud.com> Xref: csiph.com comp.lang.python:110896 > On Jul 1, 2016, at 5:46 AM, Jussi Piitulainen wrote: >=20 > Christopher Reimer writes: >=20 >> For my BASIC interpreter, each line of BASIC is broken this way into >> tokens. >>=20 >> line_number, keyword, *expression =3D line.split(' ', 2) >>=20 >> For a line like 10 PRINT "HELLO, WORLD!", this works as expected. >>=20 >> For a line like 20 END, which doesn't have a third element for >> expression, an empty list is assigned. >>=20 >> By using * to unpack the split line, my program no longer crashes and >> no try/except block is needed to work around the crash. A later line >> of code will test the expression, ignore if empty or run regex if >> full. >=20 > Yes. >=20 > Consider line.split(None, 2) or line.split(maxsplit=3D2). You may still > need to strip the third component, but at least it will *be* the third > component even if the user happens to type extra spaces like this: >=20 > 10 PRINT "Hello, world." >=20 > And you won't get a spurious third component if the user types this: >=20 > 20 END =20 >=20 > (There's trailing spaces.) This is a BASIC interpreter. Any extra spaces for line_number or keyword wil= l raise an incomprehensible syntax error, as the line number must convert to= an integer and the keyword much match the KEYWORDS list. As for trailing sp= aces for expression, regex will keep them whole if enclosed in double quotes= or convert them to a list element. Depending on the keyword grammar, extra s= paces are likely to be ignored. A C interpreter would be more difficult as the same line of code could be wr= itten several different ways and be valid. FILE *source FILE * source FILE* source FILE*source The first line is how I normally see this statement written. The last line I= found in a 1991 book with a strange dialect of C that I've never seen befor= e and doesn't always compile correctly without modification. Writing a C int= erpreter is not on my to do list.=20 Chris R.=