Version française
Home     About     Download     Resources     Contact us    
Browse thread
Toplevel function question
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Joel Christner <joel.christner@g...>
Subject: Re: Toplevel function question
Simpler representation - tried compiling this and got the same syntax error
- at a line number that was after the EOF.  Thanks in advance for any help!!

let add_text variablelist text =
  variablelist.contents <- text::variablelist.contents

let originalprogramcontents = ref [""]

let _ =
  let lexbuf = Lexing.from_channel stdin in
  let rec storeoriginalprogram =
    let expr = lexbuf in
    match expr with
    | EOF   -> ()
    | _     -> (add_text originalprogramcontents expr); storeoriginalprogram
  in let parseprogram = List.iter
    (fun n -> print_string n; print_string "\n";) originalprogramcontents

$ ocamlc -c toplevel.ml
File "toplevel.ml", line 17, characters 1-1:
Syntax error




On Wed, May 13, 2009 at 4:43 PM, Joel Christner <joel.christner@gmail.com>wrote:

> Hello,
>
> I hope you guys don't mind another beginner's question, I'm waiting on
> approval from the Yahoo! group moderator for the beginner's section.
>
> I'm trying to implement a toplevel function that will accept input from
> stdin (someone running the program will do ./programname < someinputfile),
> store it to a ref string list, and then once stored, iteratively evaluate
> each item in the list individually.
>
> What I've put together is:
>
> let _ =
>   let lexbuf = Lexing.from_channel stdin in
>   let rec storeoriginalprogram =
>     let expr = Parser.expr Scanner.token lexbuf in
>     match expr with
>     | EOF   -> ()
>     | _     -> add_text originalprogramcontents expr; storeoriginalprogram
>   in let parseprogram = List.iter
>     (fun n -> eval expr) originalprogramcontents
>
> where...
> - Parser and Scanner are already built and working great
> - When a line of text comes in, it calls 'add_text' which adds the expr
> into the 'originalprogramcontents' ref string list, and then recursively
> calls itself to get the next line
>    - let originalprogramcontents = ref [""]
>    - let add_text variablelist text = variablelist.contents <-
> text::variablelist.contents
> - Then when finished reading from stdin, iteratively executes 'eval'
> against each line in the ref string list called 'originalprogramcontents'
>
> For some reason (probably a stupid mistake on my part) it's giving me a
> syntax error and pointing to a line of code that is AFTER the end of my
> program:
>
> $ make
> ocamlc -c program.ml
> File "program.ml", line 203, characters 2-2:
> Syntax error
> make: *** [program.cmo] Error 2
>
> But my program is only 202 lines long.
>
> Any ideas on how I might go about making this work?
>
> Thanks
> Joel
>
>