The Artima Developer Community
Sponsored Link

Agile Buzz Forum
Changing the compiler

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
James Robertson

Posts: 29924
Nickname: jarober61
Registered: Jun, 2003

David Buck, Smalltalker at large
Changing the compiler Posted: May 25, 2005 6:31 AM
Reply to this message Reply

This post originated from an RSS feed registered with Agile Buzz by James Robertson.
Original Post: Changing the compiler
Feed Title: David Buck - Blog
Feed URL: http://www.cincomsmalltalk.com/rssBlog/buck-rss.xml
Feed Description: Smalltalk can do that
Latest Agile Buzz Posts
Latest Agile Buzz Posts by James Robertson
Latest Posts From David Buck - Blog

Advertisement

I was working for a company once that did a lot of work with large numbers. It's hard, though, to write 45 billion as 45000000000. It's very hard to read.Let's change the compiler to accept the syntax 45b as 45 billion. (We could easily implement the billion unary message, but that doesn't compile to a literal which means we can't put it into an array literal).

The Smalltalk compiler in VisualWorks parses the numbers through the method Number class >> readSmalltalkSyntaxFrom:. In this method, we can change the line:

value := self readIntegerFrom: aStream radix: 10.

To:

value := self readIntegerFrom: aStream radix: 10.
(aStream peekFor: $b) ifTrue: [value := value * 1000000000].
(aStream peekFor: $m) ifTrue: [value := value * 1000000].
(aStream peekFor: $k) ifTrue: [value := value * 1000].

Now we can type 45b and get 45 billion. Typing 45m gives 45 million and 45k gives 45 thousand.

What if we want 4.5b to be 4.5 billion? We just change the float parsing. In the method Number class >> readSmalltalkFloat:from: change the line:

value := integerPart + (num / den). "The exponent will be added in the next step."

to:

value := integerPart + (num / den). "The exponent will be added in the next step."
(aStream peekFor: $b) ifTrue: [value := value * 1000000000].
(aStream peekFor: $m) ifTrue: [value := value * 1000000].
(aStream peekFor: $k) ifTrue: [value := value * 1000]. 

There you go. 4.5b (even in literal arrays) means 4.5 billion. How many languages allow you to do that?

Read: Changing the compiler

Topic: Much ado about not much Previous Topic   Next Topic Topic: Sign a petition against ID cards

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use