Implementing project 7 in C# - Parser Constructor

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Implementing project 7 in C# - Parser Constructor

jsalaz1989
This post was updated on .
Hi,

I've gotten to project 7 in the book and, being a huge C# noob, I thought I'd try to implement it in this language as practice. Specifically I'm trying to follow the Parser module's specification and I'm already stumped on the first step, which states that the Constructor has:
- Arguments: Input file/stream
- Function: Opens the input file/stream and gets ready to parse it

So far, my main program (Program.cs) is:

namespace VM_1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Program Main");

            FileStream inFileStream = File.Create(@"c:\Users\<USERNAME>\Documents\Nand2Tetris\nand2tetris\projects\07\VM 1\in.vm");

            Parser parser = new Parser(inFileStream);
        }
    }
}

While the Parser module (Parser.cs) is:

namespace VM_1
{
    public class Parser
    {
        public Parser(FileStream inFileStream)
        {
            Console.WriteLine("Parser Constructor");
        }

        static void ParserMain()
        {
            Console.WriteLine("Parser Main");
        }
    }
}

I don't know if this is even the right way to structure a C# program but Program.cs and Parser.cs seem to be integrating with no issues.

The problem is that in the Parser module I'm trying to take in the filestream as an argument so that I can open it in its constructor. Therefore in Program.cs I use File.Create(), trying to reserve File.Open() for the Parser module. This is because, as specified in the book, in the Parser's constructor I would have to actually open the filestream, no? But every C# example of opening a file requires a string path as its first argument and I already supplied it in Program.cs. So I'm not sure how to implement this first step in the project. Any help is appreciated.


Thanks


Reply | Threaded
Open this post in threaded view
|

Re: Implementing project 7 in C# - Parser Constructor

Lozminda
I'm guessing, and I really am guessing, that you're not getting much feedback on your question, because it seems to be a C# question, not a Nand2Tetris question.

That is a guess mind. I can't help you any further I'm afraid. (There's no guarantee I could help you any ways, ho ho !)

Loz
Reply | Threaded
Open this post in threaded view
|

Re: Implementing project 7 in C# - Parser Constructor

jsalaz1989
Yeah, it's mostly a C# question but I had posted it here hoping somebody else had tried to implement project 7 in C# (or maybe Java).

Of course, I've also asked on Reddit and SO where the conclusion seemed to be that in C# you can't really pass in a Filestream to be opened in the constructor, since the Filestream is already open the moment you create it. 

To get as close as possible to the specs in the chapter, I opted for passing the filepath to the constructor. Although that's not really passing the file/stream in the strictest sense. 

El jue., 16 abr. 2020 19:36, Lozminda [via Nand2Tetris Questions and Answers Forum] <[hidden email]> escribió:
I'm guessing, and I really am guessing, that you're not getting much feedback on your question, because it seems to be a C# question, not a Nand2Tetris question.

That is a guess mind. I can't help you any further I'm afraid. (There's no guarantee I could help you any ways, ho ho !)

Loz


To unsubscribe from Implementing project 7 in C# - Parser Constructor, click here.
NAML
Reply | Threaded
Open this post in threaded view
|

Re: Implementing project 7 in C# - Parser Constructor

Lozminda
I, not necessarily deliberately, didn't follow any of the program guidance at all, in any of the projects.
I had some ideas about how to implement the project before I got to that stage in the book. (Usually the project is the first thing I look at of that particular chapter) and so it begins to form after a few readings of the preceding info.

Often what I come up with is fairly close to their program spec anyway.

You don't have to do it the way they say, as long as your program/assembler/vm translator etc. gives the desired
behaviour that's ok, I imagine. But then I'm not on a course or anything, just self teaching.

Sorry I can't be any more helpful.

Good luck