Re: [fpc-devel] Declare variables inside the code
Em 11/05/2011 17:01, Vinzent Höfler escreveu: On Wed, 11 May 2011 12:07:31 +0200, Hans-Peter Diettrich wrote: kingbiz...@gmail.com schrieb: I would like to be able this: [ some code after begin ] DoSomething; var I: Integer; for I := 0 to 1000 do DoAnotherThing(I); [ inside the code ] That's not good practice, in no programming language. Better move the loop into a local procedure, where you can declare really local variables. Or make the loop variable completely implicit and thus 100% local to the loop. |for i : integer := 0 to 1000 do ...; Vinzent. ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel I like this idea, but I'm changing my mind,*maybe is better to keep the current way*, its not really needed to declare a variable inside the codes, its nice, I like it but its not really necessary. But something that has been pointed (by Joerg I belive) is the initialization of variables, what about it? This would be a nice thing and I don't see why not. I prefer to see: *var X: Integer = 0; Y: Integer = 100;* than *var X: Integer; Y: Integer; begin X := 0; Y := 100;* ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Declare variables inside the code
In our previous episode, Joerg Schuelke said: > That must be some kind of misunderstanding I know the compiler > switches, I spoke about runtime debugging informations, not compile > time. And most of the switches generate compile time ones. Am I wrong? Well, that's the fun of compiler bootstrapping, it is both at the same time. ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Declare variables inside the code
On Wed, 11 May 2011 12:07:31 +0200, Hans-Peter Diettrich wrote: kingbiz...@gmail.com schrieb: I would like to be able this: [ some code after begin ] DoSomething; var I: Integer; for I := 0 to 1000 do DoAnotherThing(I); [ inside the code ] That's not good practice, in no programming language. Better move the loop into a local procedure, where you can declare really local variables. Or make the loop variable completely implicit and thus 100% local to the loop. |for i : integer := 0 to 1000 do ...; Vinzent. ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Declare variables inside the code
Am Wed, 11 May 2011 19:11:40 +0200 schrieb Hans-Peter Diettrich : > Since that time I don't wonder any more, why a C compiler > spends 50% of its time in scanning (and preprocessing) the input. I would introduce a macro expansion trough a compiler directive. Then there is no need to scan the whole program for macro occurrencies. You "only" have to preprocess the including directive and the macro body. Doing {$I %macro(p1,p2,..)} instead of simple macro(p1,p2,..) helps much. It is a clean way, and pascal like. (in my eyes) Jörg ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Declare variables inside the code
On Tue, May 10, 2011 at 1:26 PM, kingbiz...@gmail.com wrote: > I have been playing on other languages sometimes and I see some features > that speed-up a lot the code creating. I'm posting here one, I want to see > what you think about it. > > Good: fast algorithm testings, code creating > Bad?: not a standard of the pascal language > I'd love that, not for the coding speed argument, but for the maintenance argument: reducing escope thus reducing chances of error. But what I'd like the most is a compilation mode where parameters (or even local variables) are 'const' (or write-once) by default :-) Enforcing explicit declaration of every field, class or global used in a function would be nice too ;-) But please don't tell to use ADA or the new RedHat language :-D Best regards, Flávio ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Declare variables inside the code
Joerg Schuelke schrieb: Hmmm, I think about macros for pascal with parameters for some time, and ask you now: where is the debugging macro used inside the whole compiler sources? Nowhere, because it is nearly impossible to write some really useful. The parameters are not the problem. They are :-( I came across all those nasty problems, when I wrote the preprocessor for ToPas. Since that time I don't wonder any more, why a C compiler spends 50% of its time in scanning (and preprocessing) the input. DoDi ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Declare variables inside the code
Am Wed, 11 May 2011 17:56:39 +0200 schrieb Jonas Maebe : > For the compiler itself such things are toggled via command line > switches (-vc, -vt, -vu, -vd, -vp, ...). That must be some kind of misunderstanding I know the compiler switches, I spoke about runtime debugging informations, not compile time. And most of the switches generate compile time ones. Am I wrong? By the way it is not my interest to convince somebody, what he should better do :-) A little may be. Sorry for that, but in my ears it sounds a little like "I do not need, what I could not have". Sorry again. Enough about the dirty macros, the point introduced by kingbizugo was to introduce vars in code. Regards Jörg ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Declare variables inside the code
On 11 May 2011, at 17:50, Joerg Schuelke wrote: > Switched by DBGLEV:=3 for example. Programming in C I usually have a > stack of debug levels, so I can for example code: > > DBGPUSH(4); > call to whatever; > DBGPOP; > > and all that stuff vanishes with -dNODEBUG. For the compiler itself such things are toggled via command line switches (-vc, -vt, -vu, -vd, -vp, ...). Jonas___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Declare variables inside the code
Am Wed, 11 May 2011 16:56:38 +0200 schrieb Florian Klaempfl : > to get a node tree at a particular place. Having this in macros > everywhere makes imo no sense because one would gets an incredible > amount of data. OK, to make it concrete, I refer to an level driven debug system commonly used for example in the kernel sources. Lower level messages at procedure entry point, higher level output for complicated stuff. Switched by DBGLEV:=3 for example. Programming in C I usually have a stack of debug levels, so I can for example code: DBGPUSH(4); call to whatever; DBGPOP; and all that stuff vanishes with -dNODEBUG. ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Declare variables inside the code
Am 11.05.2011 14:52, schrieb Jonas Maebe: > but I'm personally not > missing anything as far as compiler development is concerned. > Me neither. And if I really need output during compilation, I add the needed code manually like printnode(self); to get a node tree at a particular place. Having this in macros everywhere makes imo no sense because one would gets an incredible amount of data. ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Declare variables inside the code
On 11 May 2011, at 15:22, Joerg Schuelke wrote: Would not it be helpful to leave the debugging code in the source, and let him vanish through a conditional define? The compiler has such code, between {$ifdef EXTDEBUG}. Having full- fledged macro support would not really make most of that code simpler, because there are few if any general patterns that appear in many places (and such patterns, at least those in the compiler, could just as easily be isolated into functions if refactoring were required). Furthermore, most debugging code is always enabled because it's much easier for a user to figure out that something is wrong when the compiler halts with "internalerror 89" instead of silently generating wrong code. Note: again, I'm only talking about the compiler sources. Jonas ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Declare variables inside the code
W dniu 2011-05-11 11:39, Joerg Schuelke pisze: Am Wed, 11 May 2011 10:21:45 +0200 (CEST) schrieb mar...@stack.nl (Marco van de Voort): FPC is much lighter on that, and never runs constructors automatically. It only initializes some pointer values to NIL. So you'll have to explain that remark in more detail. _ Yes, today. What I mean is that one decision leads to an other. 1. You decide it is nice to declare variables where you need them. 2. You find out that it is better practice to define every variable where you can initialize it, so that every var fulfills the assertions from her type, during the live time . I agree, but in different way. I'd like see pascal more declarative than imperative, and not make statement from declaration. Initialization connected with declaration looks good and we can move then to the declaration section. For example procedure foo; var t : tMyObject(); begin t.myProc; end; that can be equivalent to: procedure foo; var t : tMyObject; begin t:=tMyObject.create(); try t.myProc; finally t.free; end; end; This proposition is not only shorter, more security and possible to achieve. There are some things with similar behavior like interfaces, strings: they are initialize before statement and finalize after . Also some gain of speed can be achieve: this object can be allocate on stack, which is faster. And second: some virtual calls can be resolved and inlining. -- Darek ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Declare variables inside the code
Am Wed, 11 May 2011 15:08:45 +0200 schrieb Jonas Maebe : > I don't use any kind of automatic code generation/writing > functionality of any IDE or editor (and I edit the FPC compiler > sources with Lazarus, Text Wrangler, vim and nano depending on what > I'm doing or how I'm logged in). At least I guess that's what your > reply is referring to, because otherwise I'm not sure how it's > related to your earlier statement of "where is the debugging macro > used inside the whole compiler sources?" Sorry Jonas, thats my bad English. Some days ago I had a macro related question. About the expansion of build-in macros. And it turned out that macro is some kind of step child. The answer I got was use IDE-macros instead. Would not it be helpful to leave the debugging code in the source, and let him vanish through a conditional define? ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Declare variables inside the code
On 05/11/2011 02:52 PM, Jonas Maebe wrote: I wish I could do this with macro support, but unfortunately that's not possible so I have to do it in another and less convenient way" while working on the compiler. Some of the stuff that in ANSI C needs to (and can) be done with macros can be done with Generics. Of course C macros are exceptionally powerful, but they are really hard to use when it comes to a higher level of complexity. -Michael ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Declare variables inside the code
On 11 May 2011, at 14:59, Joerg Schuelke wrote: Am Wed, 11 May 2011 14:52:13 +0200 schrieb Jonas Maebe : but I'm personally not missing anything as far as compiler development is concerned. You use an IDE? But what without? The language is the one and the IDE is the other. There are plenty of users who do not use an IDE, or not all the time the same. I don't use any kind of automatic code generation/writing functionality of any IDE or editor (and I edit the FPC compiler sources with Lazarus, Text Wrangler, vim and nano depending on what I'm doing or how I'm logged in). At least I guess that's what your reply is referring to, because otherwise I'm not sure how it's related to your earlier statement of "where is the debugging macro used inside the whole compiler sources?" Jonas ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Declare variables inside the code
Am Wed, 11 May 2011 14:52:13 +0200 schrieb Jonas Maebe : > but I'm > personally not missing anything as far as compiler development is > concerned. You use an IDE? But what without? The language is the one and the IDE is the other. There are plenty of users who do not use an IDE, or not all the time the same. Jörg ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Declare variables inside the code
On 11 May 2011, at 14:35, Joerg Schuelke wrote: Hmmm, I think about macros for pascal with parameters for some time, and ask you now: where is the debugging macro used inside the whole compiler sources? Nowhere, because it is nearly impossible to write some really useful. I think the answer is rather "nowhere, because the developers don't have a need for them". I can't remember ever thinking "I wish I could do this with macro support, but unfortunately that's not possible so I have to do it in another and less convenient way" while working on the compiler. This obviously doesn't mean that there are no cases where other people may consider macro support to be quite handy, but I'm personally not missing anything as far as compiler development is concerned. Jonas ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Declare variables inside the code
Am Wed, 11 May 2011 12:28:29 +0200 schrieb Hans-Peter Diettrich : > Otherwise a declaration list is accepted only at the > *begin* of a compound statement, not inside a statement list: > > compound_stat: '{' [decl_list] [stat_list] '}' ; I thought that = | ... And only the kind of which is allowed is restricted in some contexts. Is it defining or only declaring. Only declaring are restricted to the beginning of the compound statement, but also defining are allowed. Jörg ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Declare variables inside the code
Am Wed, 11 May 2011 13:12:22 +0200 schrieb Hans-Peter Diettrich : > FPC macros don't have parameters, for > this and other reasons. That's not a hard restriction, because inline > procedures can be used instead, in many cases. Hmmm, I think about macros for pascal with parameters for some time, and ask you now: where is the debugging macro used inside the whole compiler sources? Nowhere, because it is nearly impossible to write some really useful. The parameters are not the problem. If you introduce a macro expansion like: {$I %macro_name(macro_parametertext)} the preprocessor knows it is a macro expansion and there is no need to check every identifier. ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Declare variables inside the code
On 05/11/2011 12:35 PM, Hans-Peter Diettrich wrote: It's a very bad idea to "think C, write Pascal", because such a translation will fail for any number of subtle differences between C and Pascal. and what about "think Pascal write C" ? Is this not Java GDR :-P . -Michael ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Declare variables inside the code
On 05/11/2011 01:12 PM, Hans-Peter Diettrich wrote: As the downside a macro can use variables without declarations, so that the coder is responsible for providing according declarations for every such macro expansion. You need to be very silly to do this :-) . -Michael ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Declare variables inside the code
Joerg Schuelke schrieb: Am Wed, 11 May 2011 00:23:13 +0200 schrieb Vinzent Höfler : I would. FPC already has interfaces. There are good reasons why most languages didn't adopt the C++-way of doing MI. I think we have two concepts - inheritance - interface You can decide for one ore the other, or possibly mix them. It is a matter of design. Or a matter of capability to implement the concept. As Vinzent mentioned, the C++ model of multiple inheritance turned out to be a misfeature. I see no reason for repeating that experiment any more. Similarly Templates and Generics - generics IMO don't suffer from some problems, that turned out after the introduction of templates. DoDi ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Declare variables inside the code
Joerg Schuelke schrieb: Ok, not the reason. But coding security is the reason, you should do so, if you can. Look at Stroustrup. The reason for pascal not to do so, is what follows from that, a really complicated stack unwinding, for example. Stack unwinding is not an argument, IMO. C++ and OPL have managed data types, that require proper stack unwinding. But there is no requirement that stack unwinding has to recurse down into local code blocks, since the compiler is free to collect all managed variables into one memory block. The use of local variables at block level has other consequences: + every loop could use "i" for the loop variable - the user (or IDE) has to figure out the declaration of every "i" DoDi ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Declare variables inside the code
Michael Schnell schrieb: IMHO the reason is reusing the variable name, which in C can be essential with complex macro declarations. You can reintroduce any variable name in a {-} block without harming the variable with the same name declared outside and used before and after the block . ACK. As the downside a macro can use variables without declarations, so that the coder is responsible for providing according declarations for every such macro expansion. That's also a frequently used feature, that can make C code even more unreadable. As a midway variables can be passed to C macros, by name, what increases the compile time considerably. FPC macros don't have parameters, for this and other reasons. That's not a hard restriction, because inline procedures can be used instead, in many cases. DoDi ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Declare variables inside the code
Joerg Schuelke schrieb: To decide that a declaration is a statement and can occur everywhere where a statement is required, is not that small you think. This has many consequences, influencing the whole code generation process. Think about exceptions and stack rewinding. Right, but a compiler can allocate local variables as appropriate. E.g. managed variables can go into the main variable block, while unmanaged variables can be overlaid. Think about efficiency of code generating, for many developers a reason for developing in pascal. Think of JIT compilers, that generate code on the fly, in no noticeable time. DoDi ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Declare variables inside the code
kingbiz...@gmail.com schrieb: I don't see any disadvantage writting a code with /C-like with pascal keywords/, shortcuts are always welcome in order to code faster (also this is the objective of the High-Level Languages, isn't?). I like Pascal and I don't see why to resist against some improvements, the language gets richer and easier. NACK Everthing should be expressable in only one way, so that standard patterns can be recognized easily when reading some code. Even if you can #define "begin" and "end" in C, and use it instead of "{" and "}", the resulting code will be unreadable. It's a very bad idea to "think C, write Pascal", because such a translation will fail for any number of subtle differences between C and Pascal. DoDi ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Declare variables inside the code
Joerg Schuelke schrieb: Am Tue, 10 May 2011 23:50:29 +0200 schrieb Hans-Peter Diettrich : The syntax would look like: Block = "BEGIN" [Declarations] {Statement} "END" . Yea, it looks like C, but it is not. The difference in C like languages is that an declaration is just a kind of statement. Not really. C allows for variable declarations outside blocks, too, as Pascal does. Otherwise a declaration list is accepted only at the *begin* of a compound statement, not inside a statement list: compound_stat: '{' [decl_list] [stat_list] '}' ; DoDi ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Declare variables inside the code
kingbiz...@gmail.com schrieb: I would like to be able this: [ some code after begin ] DoSomething; var I: Integer; for I := 0 to 1000 do DoAnotherThing(I); [ inside the code ] That's not good practice, in no programming language. Better move the loop into a local procedure, where you can declare really local variables. DoDi ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Declare variables inside the code
Am Wed, 11 May 2011 12:26:09 +0200 schrieb Michael Schnell : > Readability problems might arise regarding the scope of equally named > variables. Pascal ovoid much of this. I agree, and somebody will then come and suggest: Lets do it this way. var a:integer; begin some code using a; var a:integer; begin some code using the inner a; end; some code using the outer a; end; and we are one step closer to C++ with pascal keywords. OK, it could be done. But should it?, I think not. Jörg ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Declare variables inside the code
On 05/11/2011 11:39 AM, Joerg Schuelke wrote: 1. You decide it is nice to declare variables where you need them. Readability problems might arise regarding the scope of equally named variables. Pascal ovoid much of this. -Michael ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Declare variables inside the code
Am Wed, 11 May 2011 10:21:45 +0200 (CEST) schrieb mar...@stack.nl (Marco van de Voort): > FPC is much lighter on that, and never runs > constructors automatically. It only initializes some pointer values > to NIL. > > So you'll have to explain that remark in more detail. > _ Yes, today. What I mean is that one decision leads to an other. 1. You decide it is nice to declare variables where you need them. 2. You find out that it is better practice to define every variable where you can initialize it, so that every var fulfills the assertions from her type, during the live time . 3. You think about that, and invent standard constructors and destructors. 4. You`ll end with something which looks like C++ with pascal keywords. Development should not go from a feature to the design. The design should give you the features, which fit in it. Jörg ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Declare variables inside the code
In our previous episode, Joerg Schuelke said: > > And C did it because it wanted to save stack space in the minis of > > the early seventies. The rest is IMHO revisionism. > > Ok, not the reason. But coding security is the reason, you should do > so, if you can. Look at Stroustrup. The reason for pascal not to do so, > is what follows from that, a really complicated stack unwinding, for > example. I've no idea what you mean by that. The need for this in C++ is a heavy use of the RAII paradigm. FPC is much lighter on that, and never runs constructors automatically. It only initializes some pointer values to NIL. So you'll have to explain that remark in more detail. ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Declare variables inside the code
Am Wed, 11 May 2011 09:30:21 +0200 (CEST) schrieb mar...@stack.nl (Marco van de Voort): > In our previous episode, Joerg Schuelke said: > > The reason for C++ to say a declaration is a statement is coding > > security. > But all that was not the question, implementing the desired feature is in my eyes a further feature collecting. Language is design too, and implementing concepts rather then features. Without discussion where implementing a feature will led to in the future. Regards Jörg ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Declare variables inside the code
On 05/11/2011 09:30 AM, Marco van de Voort wrote: And C did it because it wanted to save stack space in the minis of the early seventies. Independently from the location a local variable has been declared, the compiler can decide in what section of the code it reserves stack place for it. IMHO the reason is reusing the variable name, which in C can be essential with complex macro declarations. You can reintroduce any variable name in a {-} block without harming the variable with the same name declared outside and used before and after the block . -Michael ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Declare variables inside the code
Am Wed, 11 May 2011 09:30:21 +0200 (CEST) schrieb mar...@stack.nl (Marco van de Voort): > I always thought the main reason was because C did it that way, and > C++ is C backwards compat. > > And C did it because it wanted to save stack space in the minis of > the early seventies. The rest is IMHO revisionism. Ok, not the reason. But coding security is the reason, you should do so, if you can. Look at Stroustrup. The reason for pascal not to do so, is what follows from that, a really complicated stack unwinding, for example. ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Declare variables inside the code
In our previous episode, Joerg Schuelke said: > The reason for C++ to say a declaration is a statement is coding > security. I always thought the main reason was because C did it that way, and C++ is C backwards compat. And C did it because it wanted to save stack space in the minis of the early seventies. The rest is IMHO revisionism. ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] SQLite and ftFmtBCD
or better: ftFmtBCD: begin if P.AsFMTBCD.Precision > 15 then //we are out of REAL range, so we must bind as BLOB begin s tr1=BCDTOStrP.AsFMTBCD,SQLFormatSettings); checkerror(sqlite3_bind_blob(fstatement,I,pcharstr(str1), length(str1), @freebindstring)); end else begin d o1=P.AsFloat checkerror(sqlite3_bind_double(fstatement,I,do1)); end; end; This is wrong. There are some values which can't be written as floats, but have precision<15. Joost, look at ftBCD, ftCurrency they are also written as floats So if there is it acceptable, then I would say, that also in this case is it acceptable, isn't it ? (I do not say, that it is perfect) Can you create a patch which does this? Patch is attached. DECIMAL -> ftBCD (if precision and scale is not supplied, then scale=0) DECIMAL(p) = DECIMAL(p,0) = DECIMAL(p,) -> if p<=18 then ftLargeInt else ftFmtBCD DECIMAL(p,s) -> if s>4 or p-s>14 then ftFmtBCD (number does not fit into currency) -Laco. --- sqlite3conn.pp.ori Wed May 11 07:22:28 2011 +++ sqlite3conn.pp Wed May 11 08:58:48 2011 @@ -347,11 +347,35 @@ var i : integer; FN,FD : string; ft1 : tfieldtype; - size1 : word; + size1, size2 : integer; ar1 : TStringArray; fi: integer; st: psqlite3_stmt; - + + function ExtractPrecisionAndScale(decltype: string; var precision, scale: integer): boolean; + var p: integer; + begin + p:=pos('(', decltype); + Result:=p>0; + if not Result then Exit; + System.Delete(decltype,1,p); + p:=pos(')', decltype); + Result:=p>0; + if not Result then Exit; + decltype:=copy(decltype,1,p-1); + p:=pos(',', decltype); + if p=0 then + begin + precision:=StrToIntDef(decltype, precision); + scale:=0; + end + else + begin + precision:=StrToIntDef(copy(decltype,1,p-1), precision); + scale:=StrToIntDef(copy(decltype,p+1,length(decltype)-p), scale); + end; + end; + begin st:=TSQLite3Cursor(cursor).fstatement; for i:= 0 to sqlite3_column_count(st) - 1 do @@ -379,27 +403,19 @@ begin size1:=0; case ft1 of ftString: begin -fi:=pos('(',FD); -if (fi>0) then - begin - System.Delete(FD,1,fi); - fi:=pos(')',FD); - size1:=StrToIntDef(trim(copy(FD,1,fi-1)),255); - if size1 > dsMaxStringSize then size1 := dsMaxStringSize; - end -else size1 := 255; +size1 := 255; //sql: if length is omitted then length is 1 +size2 := 0; +ExtractPrecisionAndScale(FD, size1, size2); +if size1 > dsMaxStringSize then size1 := dsMaxStringSize; end; ftBCD:begin -fi:=pos(',',FD); -if (fi>0) then - begin - System.Delete(FD,1,fi); - fi:=pos(')',FD); - size1:=StrToIntDef(trim(copy(FD,1,fi-1)),255); - if size1>4 then -ft1 := ftFMTBcd; - end -else size1 := 4; +size2 := 18; //sql: if a precision is omitted, then use implementation-defined +size1 := 0; //sql: if a scale is omitted then scale is 0 +ExtractPrecisionAndScale(FD, size2, size1); +if (size2<=18) and (size1=0) then + ft1:=ftLargeInt +else if (size2-size1>14) or (size1>4) then + ft1:=ftFmtBCD; end; ftUnknown : DatabaseError('Unknown record type: '+FN); end; // Case ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel