Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> In other words, this is a compiler.

Its not a compiler it a pre-processor.

People look at C in isolation but don't realise it was designed to be part of the full Unix system that included Sed, Awk, M4, Lex, Yacc, Sh and all the other tools.

Writing custom C abstractions with Awk is fairly trivial and you end up with efficient C code that can be further processed or tuned. Thats what tools like Awk are there for.

The compiler itself is built on the same principles doing successive transformations on source, IR, assembly etc. There is really no rigid boundary.

If you want C with custom abstractions a custom dialect that compiles to C makes perfect sense. That use case was taken into account in its design.



"Write programs that produce and generate text, because that is a universal interface" is not a good principle for a compiler. (In fact, I think it's not really a good principle all around, and Unix is worse for it, but especially for a compiler.)


The author of the article is not writing a compiler.

I fail to see how that quote has any contextual connection with the current article or compiler design. A discussion about the merits of Unix text streams or text in general is another discussion entirely then the current one.


You yourself said "there is really no rigid boundary" between a compiler and a preprocessor. By most practical definitions of a compiler, the "C amplifier" discussed in the OP is a compiler, and is definitely similar enough to suffer many of the same issues as a compiler. In particular, I think what pcwalton is getting at is that in such a "pre-processor", it's likely that you'll eventually want more expressive data structures than text can comfortable offer you.


Can you given an example of how to write custom C abstractions with Awk?



[edit] keep in mind this is just a simple example. Awk is just one tool in the toolbox.

Take a look at 'The AWK Programming Language ' Alfred V. Aho, Brian W. Kernighan, Peter J. Weinberger.

http://www.amazon.com/The-AWK-Programming-Language-Alfred/dp...

Yes, its that Aho and Kerninghan...

operators to C function mapping. you can run like so.

$ awk -f fixed.awk < fixst.in > fixed.c

--- fixed.awk ---

  BEGIN {
  	printf("#include <stdio.h>\n");
  	printf("typedef long fixed;\n");
  	printf("void *send(fixed *this, char *message, ...);\n");
  }
  /^{/ { 
  	Ndecl = 0;
  	printf("{\n");
  }
  /^}/ { 
  	while(--Ndecl >= 0)
  		printf("\t%s\n", Destructor[Ndecl]);
  	printf("}\n");
  }
  /^:/ {
  	if($2~/fixed/) {
  		sub(/;/, "", $3);
  		printf("\tfixed *%s = send(NULL, \"fixed\");\n", $3);
  		Destructor[Ndecl++] = "send(" $3 ", \"~fixed\");"
  	}
  	else if ($4 ~ /(double)/) {
  		sub(/;/, "", $5);
  		printf("\tsend(%s, \"double\", &%s);\n", $5, $2);
  	}		
    	else if ($3 ~ /^=/) {
     		sub(/;/, "", $4);
    		printf("\tsend(%s, \"=\", %s);\n", $2, $4);
    	}
    	else if ($3 ~ /^\+/) {
    		sub(/;/, "", $4);
  		printf("\tsend(%s, \"+=\", %s);\n", $2, $4);
  	}
  	else if ($3 ~ /^\*/) {
  		sub(/;/, "", $4);
  		printf("\tsend(%s, \"*=\", %s);\n", $2, $4);
  	}
  }
  /^[^{}:]/ { print }
--- fixst.in ---

  int main()
  {
  :	fixed a;
  :	fixed b;
  :	fixed t0;
  :	fixed t1;
  	double f;
  :	a  = 1.00;
  :	b  = 9.99;
  :	t0 = 0.00;
  :	t1 = 2.00;
  :	t0 += t1;
  :	t0 *= b;
  :	t0 += a;
  :	f  = (double)t0;
  }




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: