Related articles |
---|
Problems with Hardware, Languages, and Compilers hrubin@stat.purdue.edu (1997-03-07) |
Definable operators (was: Problems with Hardware, Languages, and Compi rrogers@cs.washington.edu (1997-03-09) |
Re: Definable operators (was: Problems with Hardware, Languages, and C sethml@ugcs.caltech.edu (1997-03-13) |
Re: Definable operators (was: Problems with Hardware, Languages, and C rrogers@cs.washington.edu (1997-03-13) |
Re: Definable operators (was: Problems with Hardware, Languages, and C creedy@mitretek.org (1997-03-14) |
Re: Definable operators (was: Problems with Hardware, Languages, and C nmm1@cus.cam.ac.uk (1997-03-16) |
Re: Definable operators (was: Problems with Hardware, Languages, and C andy@cs.Stanford.EDU (1997-03-16) |
Re: Definable operators (was: Problems with Hardware, Languages, and C malcolm@sedi8.nag.co.uk (1997-03-16) |
Re: Definable operators (was: Problems with Hardware, Languages, and C apardon@rc4.vub.ac.be (1997-03-18) |
[2 later articles] |
From: | sethml@ugcs.caltech.edu (Seth LaForge) |
Newsgroups: | comp.compilers,comp.lang.misc,comp.arch.arithmetic |
Date: | 13 Mar 1997 23:42:20 -0500 |
Organization: | California Institute of Technology, Pasadena |
References: | 97-03-037 97-03-043 |
Keywords: | design |
Richard Rogers <rrogers@cs.washington.edu> wrote:
>>[.... I used IMP72, a language where
>>you could add any operations you wanted, and it was awful. -John]
>
>But I've never used a
>language that allowed operator definition....
Try Haskell. Besides being a side-effect-free lazy evaluation language,
Haskell has a lot of syntactic sugar. In particular, you can define
pretty much arbitrary operators. Any token which consists entirely of
operator characters (+-*/%<>= and a few others) is considered an infix
operator by the parser. You also set the associativity and precedence:
-- Declare <<< to be a precedence 7 left-associative operator:
infixl 7 <<<
-- Type declaration: <<< takes two integers and returns an
-- integral number of the type of the first argument:
(<<<) :: (Integral a, Integral b) => a -> b -> a
-- The actual function: left bit shift:
x <<< b = x * 2^b
You can also use a regular prefix function as an infix operator if you
enclose its name in backtics. For example, the standard environment
includes a two-argument div function which performs integer division.
You can use it like (45 `div` 3).
>[IMP72 let you stick BNF in your programs so you could add any syntax
>you wanted. The problem was that everyone did, so before you could
>read an IMP72 program you first had to figure out what language it was
>written in. Experience with C++ tells me that even operator
>overloading can easily lead to unmaintainable code if you can no
>longer tell at a glance what each operator does. -John]
I suspect that Haskell doesn't lead to as many problems. There are
some pretty strict limitations on operators in Haskell - in particular
overloading of operators (all functions, really) is very limited - you
can only do it for types which conform to the same class (Integral
above is a class) - so when you use an operator you pretty much know
what you're going to get, unlike in C++. Also, the ability to use
named functions as operators means that people tend to avoid operators
whose meaning can't be easily discerned in favor of named functions.
Haskell also has a few other cool parsing features - in particular,
indentation-based grouping. All of Haskell's syntactic features seem
to be very well thought out - they allow for pretty dense code which
is still very readable. If you want to check out Haskell, I'd
recommend starting with HUGS
(http://www.cs.nott.ac.uk/Department/Staff/mpj/hugs.html), a simple
but complete Haskell interpreter (actually it compiles to bytecode,
but it's still *much* slower than a real compiler).
Seth
--
Return to the
comp.compilers page.
Search the
comp.compilers archives again.