summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Rustan Leino <leino@microsoft.com>2011-04-06 17:46:01 -0700
committerGravatar Rustan Leino <leino@microsoft.com>2011-04-06 17:46:01 -0700
commit4c4750f967d56c02bb55b2f485ddc466f1b33a58 (patch)
treedfa4a9840d2a73abe9d9dedc89511e3ffe933c92
parent17deb86f8f647808dfe38f5437c9adfb373dd7e9 (diff)
Jennisys: some initial files (with no real contents)
-rw-r--r--Jennisys/Jennisys/Ast.fs8
-rw-r--r--Jennisys/Jennisys/Jennisys.fs63
-rw-r--r--Jennisys/Jennisys/Jennisys.fsproj77
-rw-r--r--Jennisys/Jennisys/Jennisys.sln20
-rw-r--r--Jennisys/Jennisys/Lexer.fsl36
-rw-r--r--Jennisys/Jennisys/Parser.fsy45
6 files changed, 249 insertions, 0 deletions
diff --git a/Jennisys/Jennisys/Ast.fs b/Jennisys/Jennisys/Ast.fs
new file mode 100644
index 00000000..746307a4
--- /dev/null
+++ b/Jennisys/Jennisys/Ast.fs
@@ -0,0 +1,8 @@
+namespace Ast
+open System
+
+type Expr =
+ | IdLiteral of string
+ | Dot of Expr * string
+ | BinExpr of string * Expr * Expr
+
diff --git a/Jennisys/Jennisys/Jennisys.fs b/Jennisys/Jennisys/Jennisys.fs
new file mode 100644
index 00000000..6d060f3b
--- /dev/null
+++ b/Jennisys/Jennisys/Jennisys.fs
@@ -0,0 +1,63 @@
+// This project type requires the F# PowerPack at http://fsharppowerpack.codeplex.com/releases
+// Learn more about F# at http://fsharp.net
+// Original project template by Jomo Fisher based on work of Brian McNamara, Don Syme and Matt Valerio
+// This posting is provided "AS IS" with no warranties, and confers no rights.
+
+open System
+open Microsoft.FSharp.Text.Lexing
+
+open Ast
+open Lexer
+open Parser
+
+/// Evaluate a factor
+let rec evalFactor factor =
+ match factor with
+ | Float x -> x
+ | Integer x -> float x
+ | ParenEx x -> evalExpr x
+
+/// Evaluate a term
+and evalTerm term =
+ match term with
+ | Times (term, fact) -> (evalTerm term) * (evalFactor fact)
+ | Divide (term, fact) -> (evalTerm term) / (evalFactor fact)
+ | Factor fact -> evalFactor fact
+
+/// Evaluate an expression
+and evalExpr expr =
+ match expr with
+ | Plus (expr, term) -> (evalExpr expr) + (evalTerm term)
+ | Minus (expr, term) -> (evalExpr expr) - (evalTerm term)
+ | Term term -> evalTerm term
+
+/// Evaluate an equation
+and evalEquation eq =
+ match eq with
+ | Equation expr -> evalExpr expr
+
+printfn "Calculator"
+
+let rec readAndProcess() =
+ printf ":"
+ match Console.ReadLine() with
+ | "quit" -> ()
+ | expr ->
+ try
+ printfn "Lexing [%s]" expr
+ let lexbuff = LexBuffer<char>.FromString(expr)
+
+ printfn "Parsing..."
+ let equation = Parser.start Lexer.tokenize lexbuff
+
+ printfn "Evaluating Equation..."
+ let result = evalEquation equation
+
+ printfn "Result: %s" (result.ToString())
+
+ with ex ->
+ printfn "Unhandled Exception: %s" ex.Message
+
+ readAndProcess()
+
+readAndProcess() \ No newline at end of file
diff --git a/Jennisys/Jennisys/Jennisys.fsproj b/Jennisys/Jennisys/Jennisys.fsproj
new file mode 100644
index 00000000..831c56fc
--- /dev/null
+++ b/Jennisys/Jennisys/Jennisys.fsproj
@@ -0,0 +1,77 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
+ <ProductVersion>8.0.30703</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{f2ff4b3a-2fe8-474a-88df-6950f7d78908}</ProjectGuid>
+ <OutputType>Exe</OutputType>
+ <RootNamespace>Language</RootNamespace>
+ <AssemblyName>Language</AssemblyName>
+ <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+ <TargetFrameworkProfile>Client</TargetFrameworkProfile>
+ <Name>Language</Name>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <Tailcalls>false</Tailcalls>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <WarningLevel>3</WarningLevel>
+ <PlatformTarget>x86</PlatformTarget>
+ <DocumentationFile>bin\Debug\Language.XML</DocumentationFile>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <Tailcalls>true</Tailcalls>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <WarningLevel>3</WarningLevel>
+ <PlatformTarget>x86</PlatformTarget>
+ <DocumentationFile>bin\Release\Language.XML</DocumentationFile>
+ </PropertyGroup>
+ <Import Project="$(MSBuildExtensionsPath32)\..\Microsoft F#\v4.0\Microsoft.FSharp.Targets" />
+ <Import Project="$(MSBuildExtensionsPath32)\..\FSharpPowerPack-2.0.0.0\bin\FSharp.PowerPack.targets" />
+ <PropertyGroup>
+ <FsLexOutputFolder>$(IntermediateOutputPath)</FsLexOutputFolder>
+ <FsYaccOutputFolder>$(IntermediateOutputPath)</FsYaccOutputFolder>
+ </PropertyGroup>
+ <ItemGroup>
+ <Compile Include="Ast.fs" />
+ <Compile Include="$(IntermediateOutputPath)\Parser.fs">
+ <Visible>false</Visible>
+ <Link>Parser.fs</Link>
+ </Compile>
+ <Compile Include="$(IntermediateOutputPath)\Lexer.fs">
+ <Visible>false</Visible>
+ <Link>Lexer.fs</Link>
+ </Compile>
+ <FsYacc Include="Parser.fsy">
+ <OtherFlags>--module Parser</OtherFlags>
+ </FsYacc>
+ <FsLex Include="Lexer.fsl">
+ <OtherFlags>--unicode</OtherFlags>
+ </FsLex>
+ <Compile Include="Jennisys.fs" />
+ </ItemGroup>
+ <ItemGroup>
+ <Reference Include="FSharp.PowerPack">
+ <HintPath>C:\Program Files\FSharpPowerPack-1.9.9.9\bin\FSharp.PowerPack.dll</HintPath>
+ </Reference>
+ <Reference Include="mscorlib" />
+ <Reference Include="FSharp.Core" />
+ <Reference Include="System" />
+ <Reference Include="System.Core" />
+ </ItemGroup>
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project> \ No newline at end of file
diff --git a/Jennisys/Jennisys/Jennisys.sln b/Jennisys/Jennisys/Jennisys.sln
new file mode 100644
index 00000000..5f0a0968
--- /dev/null
+++ b/Jennisys/Jennisys/Jennisys.sln
@@ -0,0 +1,20 @@
+
+Microsoft Visual Studio Solution File, Format Version 11.00
+# Visual Studio 2010
+Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Jennisys", "Jennisys.fsproj", "{F2FF4B3A-2FE8-474A-88DF-6950F7D78908}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|x86 = Debug|x86
+ Release|x86 = Release|x86
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {F2FF4B3A-2FE8-474A-88DF-6950F7D78908}.Debug|x86.ActiveCfg = Debug|x86
+ {F2FF4B3A-2FE8-474A-88DF-6950F7D78908}.Debug|x86.Build.0 = Debug|x86
+ {F2FF4B3A-2FE8-474A-88DF-6950F7D78908}.Release|x86.ActiveCfg = Release|x86
+ {F2FF4B3A-2FE8-474A-88DF-6950F7D78908}.Release|x86.Build.0 = Release|x86
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+EndGlobal
diff --git a/Jennisys/Jennisys/Lexer.fsl b/Jennisys/Jennisys/Lexer.fsl
new file mode 100644
index 00000000..e3dbf766
--- /dev/null
+++ b/Jennisys/Jennisys/Lexer.fsl
@@ -0,0 +1,36 @@
+{
+module Lexer
+open System
+open Parser
+open Microsoft.FSharp.Text.Lexing
+
+let lexeme lexbuf =
+ LexBuffer<char>.LexemeString lexbuf
+}
+
+// These are some regular expression definitions
+let digit = ['0'-'9']
+let whitespace = [' ' '\t' ]
+let newline = ('\n' | '\r' '\n')
+
+rule tokenize = parse
+| whitespace { tokenize lexbuf }
+| newline { tokenize lexbuf }
+// keywords
+| "class" { CLASS }
+| "model" { MODEL }
+| "code" { CODE }
+// Operators
+| "+" { PLUS }
+| "-" { MINUS }
+| "*" { ASTER }
+| "/" { SLASH }
+// Misc
+| "(" { LPAREN }
+| ")" { RPAREN }
+// Numberic constants
+| ['-']?digit+ { INT32 (Int32.Parse(lexeme lexbuf)) }
+| ['-']?digit+('.'digit+)?(['e''E']digit+)? { FLOAT (Double.Parse(lexeme lexbuf)) }
+// EOF
+| eof { EOF }
+
diff --git a/Jennisys/Jennisys/Parser.fsy b/Jennisys/Jennisys/Parser.fsy
new file mode 100644
index 00000000..279430fe
--- /dev/null
+++ b/Jennisys/Jennisys/Parser.fsy
@@ -0,0 +1,45 @@
+%{
+
+open Ast
+
+%}
+
+// The start token becomes a parser function in the compiled code:
+%start start
+
+// These are the terminal tokens of the grammar along with the types of
+// the data carried by each token:
+%token <System.Int32> INT32
+%token <System.Double> FLOAT
+%token PLUS MINUS ASTER SLASH
+%token LPAREN RPAREN
+%token EOF
+
+// This is the type of the data produced by a successful reduction of the 'start'
+// symbol:
+%type < Ast.Equation > start
+
+%%
+
+// These are the rules of the grammar along with the F# code of the
+// actions executed as rules are reduced. In this case the actions
+// produce data using F# data construction terms.
+start: Prog { Equation($1) }
+
+Prog:
+ | Expr EOF { $1 }
+
+Expr:
+ | Expr PLUS Term { Plus($1, $3) }
+ | Expr MINUS Term { Minus($1, $3) }
+ | Term { Term($1) }
+
+Term:
+ | Term ASTER Factor { Times($1, $3) }
+ | Term SLASH Factor { Divide($1, $3) }
+ | Factor { Factor($1) }
+
+Factor:
+ | FLOAT { Float($1) }
+ | INT32 { Integer($1) }
+ | LPAREN Expr RPAREN { ParenEx($2) }