Click here to Skip to main content
12,632,959 members (34,585 online)
Click here to Skip to main content


72 bookmarked

Writing your first Domain Specific Language, Part 2 of 2

, 3 Sep 2008 CPOL
A guide to writing a compiler in .NET for beginners, using Irony.
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">

<html xmlns="">
<head runat="server">
    <title>FL to JavaScript compilation</title>
    <script type="text/javascript">
    function calculateFreight() {
		var region = getSelectedValue('regionDropdown');
		var customer = getSelectedValue('customerDropdown');
		var items = [];
		for (var i = 0; i < 4; i++) {
			var quantity = document.getElementById('quantity' + i).value;
			var weight = document.getElementById('weight' + i).value;
			items.push( { "weight": weight, "quantity": quantity } );
		var freight = getFreightCost(customer, region, items);
		alert('Freight cost is $' + (Math.round(freight*100)/100));
    function getSelectedValue(selectId) {
		var dropdown = document.getElementById(selectId);
		return dropdown.options[dropdown.selectedIndex].value;
    <style type="text/css">
			font-family: Arial;
			color: Navy;
		td input 
			width: 30px;
    <form id="form1" runat="server">
		<h1>Freight Language Demo Page</h1>
		<p>To see what this page is about, <a href="">view the article</a>.</p>
		<p id="errorMessageParagraph" style="font-weight:bold;color:Red" runat="server" visible="false" enableviewstate="false"></p>
		<asp:TextBox ID="sourceCodeTextBox" runat="server" Rows="18" TextMode="MultiLine" 
			Width="90%"></asp:TextBox><br />
		<asp:Button ID="runButton" runat="server" Text="Convert to JavaScript" onclick="runButton_Click" />
		<h2>Converted to JavaScript:</h2>
			<asp:TextBox ID="javascriptTextBox" runat="server" Rows="18" TextMode="MultiLine" 
				Width="90%" ReadOnly="true"></asp:TextBox><br />
		<h2>Sample input</h2>
			Customer type: 
			<select id="customerDropdown"><option value="vip">VIP</option><option value="normal">Normal</option></select>
			Delivery region: 
			<select id="regionDropdown"><option value="africa">Africa</option><option value="asia">Asia</option><option value="Americas">Americas</option><option value="europe">Europe</option><option value="middleeast">Middle East</option></select>
			Products ordered:
						<th>Product Name</th>
						<td><input type="text" id="quantity0" value="1" /></td>
						<td>External drive</td>
						<td><input type="hidden" id="weight0" value="1.2" />1.2</td>
						<td><input type="text" id="quantity1" value="1" /></td>
						<td><input type="hidden" id="weight1" value="0.2" />0.2</td>
						<td><input type="text" id="quantity2" value="1" /></td>
						<td><input type="hidden" id="weight2" value="10.4" />10.4</td>
						<td><input type="text" id="quantity3" value="1" /></td>
						<td><input type="hidden" id="weight3" value="5.3" />5.3</td>
			<input type="button" onclick="calculateFreight();" value="Calculate freight" />

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Daniel Flower
Software Developer
China China
Daniel has a Bachelor of Science with First Class Honours from the University of Auckland, and has designed and developed software in companies large and small.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161208.2 | Last Updated 3 Sep 2008
Article Copyright 2008 by Daniel Flower
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid